|
@@ -15,24 +15,22 @@
|
|
|
*/
|
|
|
package io.jpress.front.controller;
|
|
|
|
|
|
-import java.math.BigInteger;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
import com.jfinal.core.Controller;
|
|
|
-
|
|
|
import io.jpress.Consts;
|
|
|
import io.jpress.core.cache.ActionCache;
|
|
|
import io.jpress.model.Content;
|
|
|
-import io.jpress.model.Taxonomy;
|
|
|
import io.jpress.model.query.ContentQuery;
|
|
|
import io.jpress.model.query.OptionQuery;
|
|
|
-import io.jpress.model.query.TaxonomyQuery;
|
|
|
import io.jpress.router.RouterMapping;
|
|
|
import io.jpress.utils.DateUtils;
|
|
|
import io.jpress.utils.StringUtils;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
@RouterMapping(url = "/sitemap")
|
|
|
public class SitemapController extends Controller {
|
|
|
private static final String contentType = "text/xml; charset=" + Consts.CHARTSET_UTF8;
|
|
@@ -47,14 +45,80 @@ public class SitemapController extends Controller {
|
|
|
if (StringUtils.isBlank(domain))
|
|
|
domain = "";
|
|
|
|
|
|
- buildSitemap(xmlBuilder, domain + "/sitemap/site", format.format(new Date()));
|
|
|
- List<Taxonomy> taxonomys = TaxonomyQuery.me().findAll();
|
|
|
- if (taxonomys != null && !taxonomys.isEmpty()) {
|
|
|
- for (Taxonomy t : taxonomys) {
|
|
|
- buildSitemap(xmlBuilder, domain + "/sitemap/taxonomy/" + t.getId(), format.format(new Date()));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ int contentsSize = 0;
|
|
|
+ int page = 1;
|
|
|
+ int pageSize = 10;
|
|
|
+ do {
|
|
|
+ List<Content> contents = ContentQuery.me().findListInNormal(page, pageSize);
|
|
|
+
|
|
|
+ if (contents != null && !contents.isEmpty()) {
|
|
|
+ for (Content c : contents) {
|
|
|
+ if (c.getModified() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (c.getModule().contains("carousel")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String changefreq = "daily";
|
|
|
+ String priority = "1.0";
|
|
|
+ int dayDiff = DateUtils.getDayDiff(new Date(), c.getModified());
|
|
|
+ if (dayDiff > 30) {
|
|
|
+ changefreq = "monthly";
|
|
|
+ priority = "0.3";
|
|
|
+ } else if (dayDiff > 7) {
|
|
|
+ changefreq = "weekly";
|
|
|
+ priority = "0.9";
|
|
|
+ }
|
|
|
+
|
|
|
+ buildUrl(xmlBuilder, domain + c.getUrl(), format.format(c.getModified()), changefreq, priority);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ contentsSize = contents.size();
|
|
|
+ page ++;
|
|
|
+ } while (contentsSize == pageSize);
|
|
|
+
|
|
|
+
|
|
|
+ List<Content> contentMenus = ContentQuery.me().findListByModule("menu");
|
|
|
+
|
|
|
+ if (contentMenus != null && !contentMenus.isEmpty()) {
|
|
|
+ for (Content c : contentMenus) {
|
|
|
+ if (c.getModified() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String changefreq = "daily";
|
|
|
+ String priority = "1.0";
|
|
|
+ int dayDiff = DateUtils.getDayDiff(new Date(), c.getModified());
|
|
|
+ if (dayDiff > 30) {
|
|
|
+ changefreq = "monthly";
|
|
|
+ priority = "0.3";
|
|
|
+ } else if (dayDiff > 7) {
|
|
|
+ changefreq = "weekly";
|
|
|
+ priority = "0.9";
|
|
|
+ }
|
|
|
+
|
|
|
+ buildUrl(xmlBuilder, domain + c.getUrl(), format.format(c.getModified()), changefreq, priority);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
buildSitemapFooter(xmlBuilder);
|
|
|
+ try {
|
|
|
+ createSitemap(xmlBuilder);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
renderText(xmlBuilder.toString(), contentType);
|
|
|
}
|
|
|
|
|
@@ -154,4 +218,21 @@ public class SitemapController extends Controller {
|
|
|
xmlBuilder.append("</url>");
|
|
|
}
|
|
|
|
|
|
+ private void createSitemap(StringBuilder xmlBuilder) throws IOException {
|
|
|
+ String path = "sitemap.txt";
|
|
|
+ File file = new File(path);
|
|
|
+ if(!file.exists()){
|
|
|
+ file.createNewFile();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ OutputStreamWriter outputStreamWriterwrite = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
|
|
|
+ BufferedWriter writer = new BufferedWriter(outputStreamWriterwrite);
|
|
|
+ writer.write(xmlBuilder.toString());
|
|
|
+
|
|
|
+ writer.flush();
|
|
|
+ writer.close();
|
|
|
+ outputStreamWriterwrite.close();
|
|
|
+ }
|
|
|
+
|
|
|
}
|