MCPcopy Index your code
hub / github.com/TruthHun/DocHub / BuildSitemap

Method BuildSitemap

models/SeoModel.go:102–160  ·  view source on GitHub ↗

baseUrl := this.Ctx.Input.Scheme() + "://" + this.Ctx.Request.Host if host := beego.AppConfig.String("sitemap_host"); len(host) > 0 { baseUrl = this.Ctx.Input.Scheme() + "://" + host } 生成站点地图

()

Source from the content-addressed store, hash-verified

100//}
101//生成站点地图
102func (this *Seo) BuildSitemap() {
103 //更新站点地图
104 helper.Logger.Info(fmt.Sprintf("[%v]更新站点地图[start]", time.Now().Format("2006-01-02 15:04:05")))
105 var (
106 files []string
107 fileNum int
108 Sitemap = sitemap.NewSitemap("1.0", "utf-8")
109 si []sitemap.SitemapIndex
110 count int64
111 limit = 10000 //每个sitemap文件,限制10000个链接
112 domain = strings.ToLower(NewSys().GetByField("DomainPc").DomainPc)
113 o = orm.NewOrm()
114 )
115 if !(strings.HasPrefix(domain, "https://") || strings.HasPrefix(domain, "http://")) {
116 domain = "http://" + domain
117 }
118 domain = strings.TrimRight(domain, "/")
119 //文档总数
120 count, _ = o.QueryTable(GetTableDocumentInfo()).Filter("Status__gt", -1).Count()
121 cnt := int(count)
122 if fileNum = cnt / limit; cnt%limit > 0 {
123 fileNum = fileNum + 1
124 }
125 //创建文件夹
126 os.MkdirAll(filepath.Join(helper.RootPath, "sitemap"), os.ModePerm)
127 for i := 0; i < fileNum; i++ {
128 var docs []DocumentInfo
129 o.QueryTable(GetTableDocumentInfo()).Filter("Status__gt", -1).Limit(limit).Offset(i*limit).All(&docs, "Id", "TimeCreate")
130 if len(docs) > 0 {
131 //文件
132 file := "sitemap/doc-" + strconv.Itoa(i) + ".xml"
133 files = append(files, file)
134 var su []sitemap.SitemapUrl
135 for _, doc := range docs {
136 su = append(su, sitemap.SitemapUrl{
137 Loc: domain + "/view/" + strconv.Itoa(doc.Id),
138 Lastmod: time.Unix(int64(doc.TimeCreate), 0).Format("2006-01-02 15:04:05"),
139 ChangeFreq: sitemap.WEEKLY,
140 Priority: 0.9,
141 })
142 }
143 if err := Sitemap.CreateSitemapContent(su, filepath.Join(helper.RootPath, file)); err != nil {
144 helper.Logger.Error("sitemap生成失败:" + err.Error())
145 }
146 }
147 }
148 if len(files) > 0 {
149 for _, f := range files {
150 si = append(si, sitemap.SitemapIndex{
151 Loc: domain + "/" + f,
152 Lastmod: time.Now().Format("2006-01-02 15:04:05"),
153 })
154 }
155 }
156 if err := Sitemap.CreateSitemapIndex(si, filepath.Join(helper.RootPath, "sitemap.xml")); err != nil {
157 helper.Logger.Error("sitemap生成失败:" + err.Error())
158 }
159 helper.Logger.Info(fmt.Sprintf("[%v]更新站点地图[end]", time.Now().Format("2006-01-02 15:04:05")))

Callers 2

AutoSitemapFunction · 0.95
UpdateSitemapMethod · 0.80

Calls 5

NewSysFunction · 0.85
GetTableDocumentInfoFunction · 0.85
GetByFieldMethod · 0.80
CountMethod · 0.80
AllMethod · 0.80

Tested by

no test coverage detected