// 某个wiki页面详细信息 func FindWiki(uri string) map[string]interface{} { wiki := model.NewWiki() if err := wiki.Where("uri=" + uri).Find(); err != nil { logger.Errorln("wiki service FindWiki error:", err) return nil } uids := make(map[int]int) uids[wiki.Uid] = wiki.Uid if wiki.Cuid != "" { cuids := strings.Split(wiki.Cuid, ",") for _, cuid := range cuids { tmpUid := util.MustInt(cuid) uids[tmpUid] = tmpUid } } userMap := getUserInfos(uids) result := make(map[string]interface{}) util.Struct2Map(result, wiki) result["user"] = userMap[wiki.Uid] if wiki.Cuid != "" { cuids := strings.Split(wiki.Cuid, ",") cusers := make([]*model.User, len(cuids)) for i, cuid := range cuids { cusers[i] = userMap[util.MustInt(cuid)] } result["cuser"] = cusers } return result }
// 获得wiki列表(TODO:暂时不分页) func FindWikiList() []*model.Wiki { wikiList, err := model.NewWiki().Order("mtime DESC").FindAll("title", "uri") if err != nil { logger.Errorln("wiki service FindWikiList error:", err) return nil } return wikiList }
// 通过id获得wiki的所有者 func getWikiOwner(id int) int { wiki := model.NewWiki() err := wiki.Where("id=" + strconv.Itoa(id)).Find() if err != nil { logger.Errorln("wiki service getWikiOwner Error:", err) return 0 } return wiki.Uid }
// 获取多个wiki页面详细信息 func FindWikisByIds(ids []int) []*model.Wiki { if len(ids) == 0 { return nil } inIds := util.Join(ids, ",") wikis, err := model.NewWiki().Where("id in(" + inIds + ")").FindAll() if err != nil { logger.Errorln("wiki service FindWikisByIds error:", err) return nil } return wikis }
// 创建一个wiki页面 func CreateWiki(uid int, form url.Values) bool { wiki := model.NewWiki() err := util.ConvertAssign(wiki, form) if err != nil { logger.Errorln("wiki ConvertAssign error", err) return false } wiki.Uid = uid if _, err := wiki.Insert(); err != nil { logger.Errorln("wiki service CreateWiki error:", err) return false } // 创建一个wiki页面,活跃度+10 go IncUserWeight("uid="+strconv.Itoa(uid), 10) return true }
func GenSitemap() { sitemapFiles := []string{} // 首页 home := map[string]string{ "loc": "http://" + config.Config["domain"], "lastmode": time.Now().Format(time.RFC3339), } var ( little = 1 step = 4999 large = little + step ) // 文章 article := model.NewArticle() for { sitemapFile := "sitemap_article_" + strconv.Itoa(large) + ".xml" articles, err := article.Where("id BETWEEN ? AND ? AND status!=?", little, large, model.StatusOffline).FindAll("id", "mtime") little = large + 1 large = little + step if err != nil { continue } if len(articles) == 0 { break } data := map[string]interface{}{ "home": home, "articles": articles, } if err = output(sitemapFile, data); err == nil { sitemapFiles = append(sitemapFiles, sitemapFile) } } little = 1 large = little + step // 主题(帖子) topic := model.NewTopic() for { sitemapFile := "sitemap_topic_" + strconv.Itoa(large) + ".xml" topics, err := topic.Where("tid BETWEEN ? AND ? AND flag IN(?,?)", little, large, 0, 1).FindAll("tid", "mtime") little, large = large+1, little+step if err != nil { continue } if len(topics) == 0 { break } data := map[string]interface{}{ "home": home, "topics": topics, } if err = output(sitemapFile, data); err == nil { sitemapFiles = append(sitemapFiles, sitemapFile) } } little = 1 large = little + step // 资源 resource := model.NewResource() for { sitemapFile := "sitemap_resource_" + strconv.Itoa(large) + ".xml" resources, err := resource.Where("id BETWEEN ? AND ?", little, large).FindAll("id", "mtime") little, large = large+1, little+step if err != nil { continue } if len(resources) == 0 { break } data := map[string]interface{}{ "home": home, "resources": resources, } if err = output(sitemapFile, data); err == nil { sitemapFiles = append(sitemapFiles, sitemapFile) } } little = 1 large = little + step // 项目 project := model.NewOpenProject() for { sitemapFile := "sitemap_project_" + strconv.Itoa(large) + ".xml" projects, err := project.Where("id BETWEEN ? AND ? AND status=?", little, large, model.StatusOnline).FindAll("id", "uri", "mtime") little, large = large+1, little+step if err != nil { continue } if len(projects) == 0 { break } data := map[string]interface{}{ "home": home, "projects": projects, } if err = output(sitemapFile, data); err == nil { sitemapFiles = append(sitemapFiles, sitemapFile) } } little = 1 large = little + step // wiki wiki := model.NewWiki() for { sitemapFile := "sitemap_wiki_" + strconv.Itoa(large) + ".xml" wikis, err := wiki.Where("id BETWEEN ? AND ?", little, large).FindAll("id", "uri", "mtime") little, large = large+1, little+step if err != nil { continue } if len(wikis) == 0 { break } data := map[string]interface{}{ "home": home, "wikis": wikis, } if err = output(sitemapFile, data); err == nil { sitemapFiles = append(sitemapFiles, sitemapFile) } } file, err := os.Create(sitemapPath + "sitemapindex.xml") if err != nil { logger.Errorln("gen sitemap index file error:", err) return } defer file.Close() err = sitemapIndexTpl.Execute(file, map[string]interface{}{ "home": home, "sitemapFiles": sitemapFiles, }) if err != nil { logger.Errorln("execute sitemap index template error:", err) } }