Files
siyuan/kernel/util/runtime.go
T
游夏 36f4e3720e 🎨 Add Simplified/Traditional Chinese sensitivity option (#17846)
* ✨ 搜索支持区分繁简选项 Add Simplified/Traditional Chinese sensitivity option for search

新增搜索配置 hanSensitive(区分繁简,默认开启 = 现状行为,命名与语义
向 caseSensitive 对齐)。关闭后简体与繁体可互相检索。

- FTS 层:关闭区分繁简时,blocks_fts / blocks_fts_case_insensitive 建表
  追加 han_insensitive 分词器参数(依赖 88250/go-sqlite3 配套补丁),
  繁体在分词时按单字折叠为简体,token 偏移仍指向原文;
- 切换选项复用区分大小写现有的 FullReindex 流程;
- 高亮:EncloseHighlighting 在关闭区分繁简时将关键字逐字符展开为
  繁简等价字符类(如 诗经 -> [诗詩][经經]),与分词器使用同一份
  OpenCC TSCharacters 映射数据;
- 兼容:旧版本 conf.json 缺 hanSensitive 字段时按既往行为置为开启;
  setSearch 请求未携带该字段(现行前端/第三方调用)时保持当前值,
  避免被零值意外翻转并触发重建索引;
- 遵照配置界面重写计划(#17601),本次不改动设置界面与 i18n,仅添加
  配置数据结构字段与前端类型定义(config.d.ts)。

范围说明:本次仅覆盖块全文搜索(关键字/查询语法)的命中与高亮;
历史/资源文件搜索、数据库属性过滤、虚拟引用、查找替换的替换动作
不在本次范围内(替换保持字节精确以避免误改原文)。

Fixes #13304

* 🐛 add missing util.SearchHanSensitive referenced by highlighting and sql.SetHanSensitive

Copilot 评审指出 util.SearchHanSensitive 未定义导致无法编译:补上该全局
变量(默认 true,与既往行为一致,由 sql.SetHanSensitive 维护),并修正
conf/search.go 的 gofmt 对齐。已在本地以 dev 当前 go.mod(已合并的
88250/go-sqlite3 分词器)完成 go build -tags fts5 与 kernel/search 单测验证。
2026-06-12 15:59:50 +08:00

505 lines
13 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SiYuan - Refactor your thinking
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package util
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/88250/gulu"
"github.com/denisbrodbeck/machineid"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"github.com/jaypipes/ghw"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
)
var DisabledFeatures []string
func DisableFeature(feature string) {
DisabledFeatures = append(DisabledFeatures, feature)
DisabledFeatures = gulu.Str.RemoveDuplicatedElem(DisabledFeatures)
}
var (
UseSingleLineSave = true // UseSingleLineSave 是否使用单行保存 .sy 和数据库 .json 文件。
LargeFileWarningSize = 8 // LargeFileWarningSize 大文件警告大小,单位:MB
)
func ExceedLargeFileWarningSize(fileSize int) bool {
return fileSize > LargeFileWarningSize*1024*1024
}
// IsUILoaded 是否已经加载了 UI。
var IsUILoaded = false
func WaitForUILoaded() {
start := time.Now()
for !IsUILoaded {
time.Sleep(200 * time.Millisecond)
if time.Since(start) > 30*time.Second {
logging.LogErrorf("wait for ui loaded timeout: %s", logging.ShortStack())
break
}
}
}
func HookUILoaded() {
for !IsUILoaded {
if 0 < len(SessionsByType("main")) {
IsUILoaded = true
return
}
time.Sleep(200 * time.Millisecond)
}
}
// IsExiting 是否正在退出程序。
var IsExiting = atomic.Bool{}
// MobileOSVer 移动端操作系统版本。
var MobileOSVer string
// DatabaseVer 数据库版本。修改表结构的话需要修改这里。
const DatabaseVer = "20260610"
func logBootInfo() {
plat := GetOSPlatform()
logging.LogInfof("kernel is booting:\n"+
" * ver [%s]\n"+
" * arch [%s]\n"+
" * os [%s]\n"+
" * pid [%d]\n"+
" * runtime mode [%s]\n"+
" * working directory [%s]\n"+
" * read only [%v]\n"+
" * container [%s]\n"+
" * database [ver=%s]\n"+
" * workspace directory [%s]",
Ver, runtime.GOARCH, plat, os.Getpid(), Mode, WorkingDir, ReadOnly, Container, DatabaseVer, WorkspaceDir)
if 0 < len(DisabledFeatures) {
logging.LogInfof("disabled features [%s]", strings.Join(DisabledFeatures, ", "))
}
go func() {
driveType := getWorkspaceDriveType()
if "" == driveType {
return
}
if ghw.DriveTypeSSD.String() != driveType {
logging.LogWarnf("workspace dir [%s] is not in SSD drive, performance may be affected", WorkspaceDir)
WaitForUILoaded()
time.Sleep(3 * time.Second)
PushErrMsg(Langs[Lang][278], 15000)
}
}()
}
func getWorkspaceDriveType() string {
if gulu.OS.IsDarwin() {
return ghw.DriveTypeSSD.String()
}
if IsMobileContainer() {
return ghw.DriveTypeSSD.String()
}
block, err := ghw.Block()
if err != nil {
logging.LogWarnf("get block storage info failed: %s", err)
return ""
}
var maxMountPathLen int
var matchedDriveType string
parentRelPrefix := ".." + string(filepath.Separator)
workspacePath := filepath.Clean(WorkspaceDir)
if gulu.OS.IsWindows() {
vol := strings.ToLower(filepath.VolumeName(workspacePath))
for _, disk := range block.Disks {
for _, partition := range disk.Partitions {
if strings.EqualFold(strings.TrimSuffix(partition.MountPoint, "\\"), vol) {
return partition.Disk.DriveType.String()
}
}
}
} else if gulu.OS.IsLinux() {
for _, disk := range block.Disks {
for _, partition := range disk.Partitions {
if partition.MountPoint == "" {
continue
}
mountPath := filepath.Clean(partition.MountPoint)
rel, err := filepath.Rel(mountPath, workspacePath)
if err != nil {
continue
}
if rel == ".." || strings.HasPrefix(rel, parentRelPrefix) {
continue
}
// 选路径最长的挂载点(如 /home/data 优于 /)
if len(mountPath) >= maxMountPathLen {
maxMountPathLen = len(mountPath)
matchedDriveType = partition.Disk.DriveType.String()
}
}
}
}
return matchedDriveType
}
func RandomSleep(minMills, maxMills int) {
r := gulu.Rand.Int(minMills, maxMills)
time.Sleep(time.Duration(r) * time.Millisecond)
}
func GetDeviceID() string {
if ContainerStd == Container {
machineID, err := machineid.ID()
if err != nil {
return gulu.Rand.String(12)
}
return machineID
}
return gulu.Rand.String(12)
}
func GetDeviceName() string {
ret, err := os.Hostname()
if err != nil {
return "unknown"
}
return ret
}
func SetNetworkProxy(proxyURL string) {
if err := os.Setenv("HTTPS_PROXY", proxyURL); err != nil {
logging.LogErrorf("set env [HTTPS_PROXY] failed: %s", err)
}
if err := os.Setenv("HTTP_PROXY", proxyURL); err != nil {
logging.LogErrorf("set env [HTTP_PROXY] failed: %s", err)
}
if "" != proxyURL {
logging.LogInfof("use network proxy [%s]", proxyURL)
} else {
logging.LogInfof("use network proxy [system]")
}
httpclient.CloseIdleConnections()
}
const (
// SQLFlushInterval 为数据库事务队列写入间隔。
SQLFlushInterval = 3000 * time.Millisecond
)
var (
Langs = map[string]map[int]string{}
TimeLangs = map[string]map[string]any{}
TaskActionLangs = map[string]map[string]any{}
TrayMenuLangs = map[string]map[string]any{}
AttrViewLangs = map[string]map[string]any{}
)
var (
thirdPartySyncCheckTicker = time.NewTicker(time.Minute * 10)
)
func ReportFileSysFatalError(err error) {
stack := debug.Stack()
output := string(stack)
if 5 < strings.Count(output, "\n") {
lines := strings.Split(output, "\n")
output = strings.Join(lines[5:], "\n")
}
logging.LogErrorf("check file system status failed: %s, %s", err, output)
os.Exit(logging.ExitCodeFileSysErr)
}
var checkFileSysStatusLock = sync.Mutex{}
func CheckFileSysStatus() {
if ContainerStd != Container {
return
}
for {
<-thirdPartySyncCheckTicker.C
checkFileSysStatus()
}
}
func checkFileSysStatus() {
defer logging.Recover()
if !checkFileSysStatusLock.TryLock() {
logging.LogWarnf("check file system status is locked, skip")
return
}
defer checkFileSysStatusLock.Unlock()
const fileSysStatusCheckFile = ".siyuan/filesys_status_check"
if IsCloudDrivePath(WorkspaceDir) {
ReportFileSysFatalError(fmt.Errorf("workspace dir [%s] is in third party sync dir", WorkspaceDir))
return
}
dir := filepath.Join(DataDir, fileSysStatusCheckFile)
if err := os.RemoveAll(dir); err != nil {
ReportFileSysFatalError(err)
return
}
if err := os.MkdirAll(dir, 0755); err != nil {
ReportFileSysFatalError(err)
return
}
for i := 0; i < 7; i++ {
tmp := filepath.Join(dir, "check_consistency")
data := make([]byte, 1024*4)
_, err := rand.Read(data)
if err != nil {
ReportFileSysFatalError(err)
return
}
if err = os.WriteFile(tmp, data, 0644); err != nil {
ReportFileSysFatalError(err)
return
}
time.Sleep(5 * time.Second)
for j := 0; j < 32; j++ {
renamed := tmp + "_renamed"
if err = os.Rename(tmp, renamed); err != nil {
ReportFileSysFatalError(err)
break
}
RandomSleep(500, 1000)
f, err := os.Open(renamed)
if err != nil {
ReportFileSysFatalError(err)
return
}
if err = f.Close(); err != nil {
ReportFileSysFatalError(err)
return
}
if err = os.Rename(renamed, tmp); err != nil {
ReportFileSysFatalError(err)
return
}
entries, err := os.ReadDir(dir)
if err != nil {
ReportFileSysFatalError(err)
return
}
checkFilenames := bytes.Buffer{}
for _, entry := range entries {
if !entry.IsDir() && strings.Contains(entry.Name(), "check_") {
checkFilenames.WriteString(entry.Name())
checkFilenames.WriteString("\n")
}
}
lines := strings.Split(strings.TrimSpace(checkFilenames.String()), "\n")
if 1 < len(lines) {
buf := bytes.Buffer{}
for _, line := range lines {
buf.WriteString(" ")
buf.WriteString(line)
buf.WriteString("\n")
}
output := buf.String()
ReportFileSysFatalError(fmt.Errorf("dir [%s] has more than 1 file:\n%s", dir, output))
return
}
}
if err = os.RemoveAll(tmp); err != nil {
ReportFileSysFatalError(err)
return
}
}
}
func IsCloudDrivePath(workspaceAbsPath string) bool {
if isICloudPath(workspaceAbsPath) {
return true
}
if isKnownCloudDrivePath(workspaceAbsPath) {
return true
}
if existAvailabilityStatus(workspaceAbsPath) {
return true
}
return false
}
func isKnownCloudDrivePath(workspaceAbsPath string) bool {
workspaceAbsPathLower := strings.ToLower(workspaceAbsPath)
return strings.Contains(workspaceAbsPathLower, "onedrive") || strings.Contains(workspaceAbsPathLower, "dropbox") ||
strings.Contains(workspaceAbsPathLower, "google drive") || strings.Contains(workspaceAbsPathLower, "pcloud") ||
strings.Contains(workspaceAbsPathLower, "坚果云") ||
strings.Contains(workspaceAbsPathLower, "天翼云")
}
func isICloudPath(workspaceAbsPath string) (ret bool) {
if !gulu.OS.IsDarwin() {
return false
}
workspaceAbsPathLower := strings.ToLower(workspaceAbsPath)
// macOS 端对工作空间放置在 iCloud 路径下做检查 https://github.com/siyuan-note/siyuan/issues/7747
iCloudRoot := filepath.Join(HomeDir, "Library", "Mobile Documents")
WalkWithSymlinks(iCloudRoot, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
return nil
}
if strings.HasPrefix(workspaceAbsPathLower, strings.ToLower(path)) {
ret = true
logging.LogWarnf("workspace [%s] is in iCloud path [%s]", workspaceAbsPath, path)
return io.EOF
}
return nil
})
return
}
func existAvailabilityStatus(workspaceAbsPath string) bool {
if !gulu.OS.IsWindows() {
return false
}
if !gulu.File.IsExist(workspaceAbsPath) {
return false
}
// 改进 Windows 端第三方同步盘检测 https://github.com/siyuan-note/siyuan/issues/7777
defer logging.Recover()
checkAbsPath := filepath.Join(workspaceAbsPath, "data")
if !gulu.File.IsExist(checkAbsPath) {
checkAbsPath = workspaceAbsPath
}
if !gulu.File.IsExist(checkAbsPath) {
logging.LogWarnf("check path [%s] not exist", checkAbsPath)
return false
}
runtime.LockOSThread()
defer runtime.LockOSThread()
if err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED); err != nil {
logging.LogWarnf("initialize ole failed: %s", err)
return false
}
defer ole.CoUninitialize()
dir, file := filepath.Split(checkAbsPath)
unknown, err := oleutil.CreateObject("Shell.Application")
if err != nil {
logging.LogWarnf("create shell application failed: %s", err)
return false
}
shell, err := unknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
logging.LogWarnf("query shell interface failed: %s", err)
return false
}
defer shell.Release()
result, err := oleutil.CallMethod(shell, "NameSpace", dir)
if err != nil {
logging.LogWarnf("call shell [NameSpace] failed: %s", err)
return false
}
folderObj := result.ToIDispatch()
result, err = oleutil.CallMethod(folderObj, "ParseName", file)
if err != nil {
logging.LogWarnf("call shell [ParseName] failed: %s", err)
return false
}
fileObj := result.ToIDispatch()
if nil == fileObj {
logging.LogWarnf("call shell [ParseName] file is nil [%s]", checkAbsPath)
return false
}
result, err = oleutil.CallMethod(folderObj, "GetDetailsOf", fileObj, 303)
if err != nil {
logging.LogWarnf("call shell [GetDetailsOf] failed: %s", err)
return false
}
value := result
if nil == value {
return false
}
status := strings.ToLower(value.ToString())
if "" == status || "availability status" == status || "可用性状态" == status {
return false
}
if strings.Contains(status, "sync") || strings.Contains(status, "同步") ||
strings.Contains(status, "available on this device") || strings.Contains(status, "在此设备上可用") ||
strings.Contains(status, "available when online") || strings.Contains(status, "联机时可用") {
logging.LogErrorf("[%s] third party sync status [%s]", checkAbsPath, status)
return true
}
return false
}
const (
EvtConfPandocInitialized = "conf.pandoc.initialized"
EvtSQLHistoryRebuild = "sql.history.rebuild"
EvtSQLAssetContentRebuild = "sql.assetContent.rebuild"
)
var SearchCaseSensitive bool
// SearchHanSensitive 是否区分繁简,由 sql.SetHanSensitive 维护;默认 true 与既往行为一致
var SearchHanSensitive = true