search.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "net/http"
  19. "strings"
  20. "github.com/88250/gulu"
  21. "github.com/gin-gonic/gin"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func listInvalidBlockRefs(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. arg, ok := util.JsonArg(c, ret)
  29. if !ok {
  30. return
  31. }
  32. page := 1
  33. if nil != arg["page"] {
  34. page = int(arg["page"].(float64))
  35. }
  36. if 0 >= page {
  37. page = 1
  38. }
  39. pageSize := 32
  40. if nil != arg["pageSize"] {
  41. pageSize = int(arg["pageSize"].(float64))
  42. }
  43. if 0 >= pageSize {
  44. pageSize = 32
  45. }
  46. blocks, matchedBlockCount, matchedRootCount, pageCount := model.ListInvalidBlockRefs(page, pageSize)
  47. ret.Data = map[string]interface{}{
  48. "blocks": blocks,
  49. "matchedBlockCount": matchedBlockCount,
  50. "matchedRootCount": matchedRootCount,
  51. "pageCount": pageCount,
  52. }
  53. }
  54. func getAssetContent(c *gin.Context) {
  55. ret := gulu.Ret.NewResult()
  56. defer c.JSON(http.StatusOK, ret)
  57. arg, ok := util.JsonArg(c, ret)
  58. if !ok {
  59. return
  60. }
  61. id := arg["id"].(string)
  62. query := arg["query"].(string)
  63. queryMethod := int(arg["queryMethod"].(float64))
  64. ret.Data = map[string]interface{}{
  65. "assetContent": model.GetAssetContent(id, query, queryMethod),
  66. }
  67. return
  68. }
  69. func fullTextSearchAssetContent(c *gin.Context) {
  70. ret := gulu.Ret.NewResult()
  71. defer c.JSON(http.StatusOK, ret)
  72. arg, ok := util.JsonArg(c, ret)
  73. if !ok {
  74. return
  75. }
  76. if !model.IsPaidUser() {
  77. ret.Code = 1
  78. return
  79. }
  80. page, pageSize, query, types, method, orderBy := parseSearchAssetContentArgs(arg)
  81. assetContents, matchedAssetCount, pageCount := model.FullTextSearchAssetContent(query, types, method, orderBy, page, pageSize)
  82. ret.Data = map[string]interface{}{
  83. "assetContents": assetContents,
  84. "matchedAssetCount": matchedAssetCount,
  85. "pageCount": pageCount,
  86. }
  87. }
  88. func findReplace(c *gin.Context) {
  89. ret := gulu.Ret.NewResult()
  90. defer c.JSON(http.StatusOK, ret)
  91. arg, ok := util.JsonArg(c, ret)
  92. if !ok {
  93. return
  94. }
  95. _, _, _, paths, boxes, types, method, orderBy, groupBy := parseSearchBlockArgs(arg)
  96. k := arg["k"].(string)
  97. r := arg["r"].(string)
  98. idsArg := arg["ids"].([]interface{})
  99. var ids []string
  100. for _, id := range idsArg {
  101. ids = append(ids, id.(string))
  102. }
  103. replaceTypes := map[string]bool{}
  104. // text, imgText, imgTitle, imgSrc, aText, aTitle, aHref, code, em, strong, inlineMath, inlineMemo, kbd, mark, s, sub, sup, tag, u
  105. // docTitle, codeBlock, mathBlock, htmlBlock
  106. if nil != arg["replaceTypes"] {
  107. replaceTypesArg := arg["replaceTypes"].(map[string]interface{})
  108. for t, b := range replaceTypesArg {
  109. replaceTypes[t] = b.(bool)
  110. }
  111. }
  112. err := model.FindReplace(k, r, replaceTypes, ids, paths, boxes, types, method, orderBy, groupBy)
  113. if nil != err {
  114. ret.Code = 1
  115. ret.Msg = err.Error()
  116. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  117. return
  118. }
  119. return
  120. }
  121. func searchAsset(c *gin.Context) {
  122. ret := gulu.Ret.NewResult()
  123. defer c.JSON(http.StatusOK, ret)
  124. arg, ok := util.JsonArg(c, ret)
  125. if !ok {
  126. return
  127. }
  128. k := arg["k"].(string)
  129. var exts []string
  130. if extsArg := arg["exts"]; nil != extsArg {
  131. for _, ext := range extsArg.([]interface{}) {
  132. exts = append(exts, ext.(string))
  133. }
  134. }
  135. ret.Data = model.SearchAssetsByName(k, exts)
  136. return
  137. }
  138. func searchTag(c *gin.Context) {
  139. ret := gulu.Ret.NewResult()
  140. defer c.JSON(http.StatusOK, ret)
  141. arg, ok := util.JsonArg(c, ret)
  142. if !ok {
  143. return
  144. }
  145. k := arg["k"].(string)
  146. tags := model.SearchTags(k)
  147. if 1 > len(tags) {
  148. tags = []string{}
  149. }
  150. ret.Data = map[string]interface{}{
  151. "tags": tags,
  152. "k": k,
  153. }
  154. }
  155. func searchWidget(c *gin.Context) {
  156. ret := gulu.Ret.NewResult()
  157. defer c.JSON(http.StatusOK, ret)
  158. arg, ok := util.JsonArg(c, ret)
  159. if !ok {
  160. return
  161. }
  162. keyword := arg["k"].(string)
  163. blocks := model.SearchWidget(keyword)
  164. ret.Data = map[string]interface{}{
  165. "blocks": blocks,
  166. "k": keyword,
  167. }
  168. }
  169. func removeTemplate(c *gin.Context) {
  170. ret := gulu.Ret.NewResult()
  171. defer c.JSON(http.StatusOK, ret)
  172. arg, ok := util.JsonArg(c, ret)
  173. if !ok {
  174. return
  175. }
  176. path := arg["path"].(string)
  177. err := model.RemoveTemplate(path)
  178. if nil != err {
  179. ret.Code = -1
  180. ret.Msg = err.Error()
  181. return
  182. }
  183. }
  184. func searchTemplate(c *gin.Context) {
  185. ret := gulu.Ret.NewResult()
  186. defer c.JSON(http.StatusOK, ret)
  187. arg, ok := util.JsonArg(c, ret)
  188. if !ok {
  189. return
  190. }
  191. keyword := arg["k"].(string)
  192. blocks := model.SearchTemplate(keyword)
  193. ret.Data = map[string]interface{}{
  194. "blocks": blocks,
  195. "k": keyword,
  196. }
  197. }
  198. func getEmbedBlock(c *gin.Context) {
  199. // Query embed block supports executing JavaScript https://github.com/siyuan-note/siyuan/issues/9648
  200. ret := gulu.Ret.NewResult()
  201. defer c.JSON(http.StatusOK, ret)
  202. arg, ok := util.JsonArg(c, ret)
  203. if !ok {
  204. return
  205. }
  206. embedBlockID := arg["embedBlockID"].(string)
  207. includeIDsArg := arg["includeIDs"].([]interface{})
  208. var includeIDs []string
  209. for _, includeID := range includeIDsArg {
  210. includeIDs = append(includeIDs, includeID.(string))
  211. }
  212. headingMode := 0 // 0:带标题下方块
  213. headingModeArg := arg["headingMode"]
  214. if nil != headingModeArg {
  215. headingMode = int(headingModeArg.(float64))
  216. }
  217. breadcrumb := false
  218. breadcrumbArg := arg["breadcrumb"]
  219. if nil != breadcrumbArg {
  220. breadcrumb = breadcrumbArg.(bool)
  221. }
  222. blocks := model.GetEmbedBlock(embedBlockID, includeIDs, headingMode, breadcrumb)
  223. ret.Data = map[string]interface{}{
  224. "blocks": blocks,
  225. }
  226. }
  227. func updateEmbedBlock(c *gin.Context) {
  228. ret := gulu.Ret.NewResult()
  229. defer c.JSON(http.StatusOK, ret)
  230. arg, ok := util.JsonArg(c, ret)
  231. if !ok {
  232. return
  233. }
  234. id := arg["id"].(string)
  235. content := arg["content"].(string)
  236. err := model.UpdateEmbedBlock(id, content)
  237. if nil != err {
  238. ret.Code = -1
  239. ret.Msg = err.Error()
  240. return
  241. }
  242. }
  243. func searchEmbedBlock(c *gin.Context) {
  244. ret := gulu.Ret.NewResult()
  245. defer c.JSON(http.StatusOK, ret)
  246. arg, ok := util.JsonArg(c, ret)
  247. if !ok {
  248. return
  249. }
  250. embedBlockID := arg["embedBlockID"].(string)
  251. stmt := arg["stmt"].(string)
  252. excludeIDsArg := arg["excludeIDs"].([]interface{})
  253. var excludeIDs []string
  254. for _, excludeID := range excludeIDsArg {
  255. excludeIDs = append(excludeIDs, excludeID.(string))
  256. }
  257. headingMode := 0 // 0:带标题下方块
  258. headingModeArg := arg["headingMode"]
  259. if nil != headingModeArg {
  260. headingMode = int(headingModeArg.(float64))
  261. }
  262. breadcrumb := false
  263. breadcrumbArg := arg["breadcrumb"]
  264. if nil != breadcrumbArg {
  265. breadcrumb = breadcrumbArg.(bool)
  266. }
  267. blocks := model.SearchEmbedBlock(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb)
  268. ret.Data = map[string]interface{}{
  269. "blocks": blocks,
  270. }
  271. }
  272. func searchRefBlock(c *gin.Context) {
  273. ret := gulu.Ret.NewResult()
  274. defer c.JSON(http.StatusOK, ret)
  275. arg, ok := util.JsonArg(c, ret)
  276. if !ok {
  277. return
  278. }
  279. reqId := arg["reqId"]
  280. ret.Data = map[string]interface{}{"reqId": reqId}
  281. if nil == arg["id"] {
  282. return
  283. }
  284. isSquareBrackets := false
  285. if isSquareBracketsArg := arg["isSquareBrackets"]; nil != isSquareBracketsArg {
  286. isSquareBrackets = isSquareBracketsArg.(bool)
  287. }
  288. isDatabase := false
  289. if isDatabaseArg := arg["isDatabase"]; nil != isDatabaseArg {
  290. isDatabase = isDatabaseArg.(bool)
  291. }
  292. rootID := arg["rootID"].(string)
  293. id := arg["id"].(string)
  294. keyword := arg["k"].(string)
  295. beforeLen := int(arg["beforeLen"].(float64))
  296. blocks, newDoc := model.SearchRefBlock(id, rootID, keyword, beforeLen, isSquareBrackets, isDatabase)
  297. ret.Data = map[string]interface{}{
  298. "blocks": blocks,
  299. "newDoc": newDoc,
  300. "k": util.EscapeHTML(keyword),
  301. "reqId": arg["reqId"],
  302. }
  303. }
  304. func fullTextSearchBlock(c *gin.Context) {
  305. ret := gulu.Ret.NewResult()
  306. defer c.JSON(http.StatusOK, ret)
  307. arg, ok := util.JsonArg(c, ret)
  308. if !ok {
  309. return
  310. }
  311. page, pageSize, query, paths, boxes, types, method, orderBy, groupBy := parseSearchBlockArgs(arg)
  312. blocks, matchedBlockCount, matchedRootCount, pageCount := model.FullTextSearchBlock(query, boxes, paths, types, method, orderBy, groupBy, page, pageSize)
  313. ret.Data = map[string]interface{}{
  314. "blocks": blocks,
  315. "matchedBlockCount": matchedBlockCount,
  316. "matchedRootCount": matchedRootCount,
  317. "pageCount": pageCount,
  318. }
  319. }
  320. func parseSearchBlockArgs(arg map[string]interface{}) (page, pageSize int, query string, paths, boxes []string, types map[string]bool, method, orderBy, groupBy int) {
  321. page = 1
  322. if nil != arg["page"] {
  323. page = int(arg["page"].(float64))
  324. }
  325. if 0 >= page {
  326. page = 1
  327. }
  328. pageSize = 32
  329. if nil != arg["pageSize"] {
  330. pageSize = int(arg["pageSize"].(float64))
  331. }
  332. if 0 >= pageSize {
  333. pageSize = 32
  334. }
  335. queryArg := arg["query"]
  336. if nil != queryArg {
  337. query = queryArg.(string)
  338. }
  339. pathsArg := arg["paths"]
  340. if nil != pathsArg {
  341. for _, p := range pathsArg.([]interface{}) {
  342. path := p.(string)
  343. box := strings.TrimSpace(strings.Split(path, "/")[0])
  344. if "" != box {
  345. boxes = append(boxes, box)
  346. }
  347. path = strings.TrimSpace(strings.TrimPrefix(path, box))
  348. if "" != path {
  349. paths = append(paths, path)
  350. }
  351. }
  352. paths = gulu.Str.RemoveDuplicatedElem(paths)
  353. boxes = gulu.Str.RemoveDuplicatedElem(boxes)
  354. }
  355. if nil != arg["types"] {
  356. typesArg := arg["types"].(map[string]interface{})
  357. types = map[string]bool{}
  358. for t, b := range typesArg {
  359. types[t] = b.(bool)
  360. }
  361. }
  362. // method:0:关键字,1:查询语法,2:SQL,3:正则表达式
  363. methodArg := arg["method"]
  364. if nil != methodArg {
  365. method = int(methodArg.(float64))
  366. }
  367. // orderBy:0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时),6:按相关度升序,7:按相关度降序
  368. orderByArg := arg["orderBy"]
  369. if nil != orderByArg {
  370. orderBy = int(orderByArg.(float64))
  371. }
  372. // groupBy: 0:不分组,1:按文档分组
  373. groupByArg := arg["groupBy"]
  374. if nil != groupByArg {
  375. groupBy = int(groupByArg.(float64))
  376. }
  377. return
  378. }
  379. func parseSearchAssetContentArgs(arg map[string]interface{}) (page, pageSize int, query string, types map[string]bool, method, orderBy int) {
  380. page = 1
  381. if nil != arg["page"] {
  382. page = int(arg["page"].(float64))
  383. }
  384. if 0 >= page {
  385. page = 1
  386. }
  387. pageSize = 32
  388. if nil != arg["pageSize"] {
  389. pageSize = int(arg["pageSize"].(float64))
  390. }
  391. if 0 >= pageSize {
  392. pageSize = 32
  393. }
  394. queryArg := arg["query"]
  395. if nil != queryArg {
  396. query = queryArg.(string)
  397. }
  398. if nil != arg["types"] {
  399. typesArg := arg["types"].(map[string]interface{})
  400. types = map[string]bool{}
  401. for t, b := range typesArg {
  402. types[t] = b.(bool)
  403. }
  404. }
  405. // method:0:关键字,1:查询语法,2:SQL,3:正则表达式
  406. methodArg := arg["method"]
  407. if nil != methodArg {
  408. method = int(methodArg.(float64))
  409. }
  410. // orderBy:0:按相关度降序,1:按相关度升序,2:按更新时间升序,3:按更新时间降序
  411. orderByArg := arg["orderBy"]
  412. if nil != orderByArg {
  413. orderBy = int(orderByArg.(float64))
  414. }
  415. return
  416. }