From 7363c711809f3e326e7fb29b8e5a3be391b2b018 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 31 Jan 2015 13:37:31 -0500 Subject: [PATCH] sitemap style --- src/sitemap.js | 200 ++++++++++++++++++++++++++----------------------- 1 file changed, 106 insertions(+), 94 deletions(-) diff --git a/src/sitemap.js b/src/sitemap.js index 16452a8f30..aab7451a61 100644 --- a/src/sitemap.js +++ b/src/sitemap.js @@ -10,109 +10,121 @@ var path = require('path'), topics = require('./topics'), privileges = require('./privileges'), meta = require('./meta'), - utils = require('../public/src/utils'), - sitemap = { - obj: undefined, - getStaticUrls: function(callback) { - callback(null, [{ - url: '', - changefreq: 'weekly', - priority: '0.6' - }, { - url: '/recent', - changefreq: 'daily', - priority: '0.4' - }, { - url: '/users', - changefreq: 'daily', - priority: '0.4' - }]); - }, - getDynamicUrls: function(callback) { - var returnUrls = []; - - async.parallel({ - categoryUrls: function(next) { - var categoryUrls = []; - categories.getCategoriesByPrivilege(0, 'find', function(err, categoriesData) { - if (err) { - return next(err); - } - - categoriesData.forEach(function(category) { - if (category) { - categoryUrls.push({ - url: '/category/' + category.cid + '/' + encodeURIComponent(utils.slugify(category.name)), - changefreq: 'weekly', - priority: '0.4' - }); - } - }); + utils = require('../public/src/utils'); - next(null, categoryUrls); - }); - }, - topicUrls: function(next) { - var topicUrls = []; - - async.waterfall([ - function(next) { - db.getSortedSetRevRange('topics:recent', 0, parseInt(meta.config.sitemapTopics, 10) || -1, next); - }, - function(tids, next) { - privileges.topics.filter('read', tids, 0, next); - }, - function(tids, next) { - topics.getTopicsFields(tids, ['tid', 'title', 'lastposttime'], next); - } - ], function(err, topics) { - if (err) { - return next(err); - } - - topics.forEach(function(topic) { - if (topic) { - topicUrls.push({ - url: '/topic/' + topic.tid + '/' + encodeURIComponent(utils.slugify(topic.title)), - lastmodISO: utils.toISOString(topic.lastposttime), - changefreq: 'daily', - priority: '0.6' - }); - } - }); +var sitemap = {}; - next(null, topicUrls); - }); - } - }, function(err, data) { - if (!err) { - returnUrls = data.categoryUrls.concat(data.topicUrls); +sitemap.render = function(callback) { + if (sitemap.obj && sitemap.obj.cache.length) { + return sitemap.obj.toXML(callback); + } + + async.parallel([ + sitemap.getStaticUrls, + sitemap.getDynamicUrls + ], function(err, urls) { + if (err) { + return callback(err); + } + + urls = urls[0].concat(urls[1]); + + sitemap.obj = sm.createSitemap({ + hostname: nconf.get('url'), + cacheTime: 1000 * 60 * 60, // Cached for 1 hour + urls: urls + }); + + sitemap.obj.toXML(callback); + }); +}; + +sitemap.getStaticUrls = function(callback) { + callback(null, [{ + url: '', + changefreq: 'weekly', + priority: '0.6' + }, { + url: '/recent', + changefreq: 'daily', + priority: '0.4' + }, { + url: '/users', + changefreq: 'daily', + priority: '0.4' + }]); +}; + +sitemap.getDynamicUrls = function(callback) { + var returnUrls = []; + + async.parallel({ + categoryUrls: function(next) { + var categoryUrls = []; + categories.getCategoriesByPrivilege(0, 'find', function(err, categoriesData) { + if (err) { + return next(err); } - callback(err, returnUrls); + categoriesData.forEach(function(category) { + if (category) { + categoryUrls.push({ + url: '/category/' + category.cid + '/' + encodeURIComponent(utils.slugify(category.name)), + changefreq: 'weekly', + priority: '0.4' + }); + } + }); + + next(null, categoryUrls); }); }, - render: function(callback) { - if (sitemap.obj !== undefined && sitemap.obj.cache.length) { - return sitemap.obj.toXML(callback); - } - - async.parallel([sitemap.getStaticUrls, sitemap.getDynamicUrls], function(err, urls) { - urls = urls[0].concat(urls[1]); - sitemap.obj = sm.createSitemap({ - hostname: nconf.get('url'), - cacheTime: 1000 * 60 * 60, // Cached for 1 hour - urls: urls + topicUrls: function(next) { + var topicUrls = []; + + async.waterfall([ + function(next) { + db.getSortedSetRevRange('topics:recent', 0, parseInt(meta.config.sitemapTopics, 10) || -1, next); + }, + function(tids, next) { + privileges.topics.filter('read', tids, 0, next); + }, + function(tids, next) { + topics.getTopicsFields(tids, ['tid', 'title', 'lastposttime'], next); + } + ], function(err, topics) { + if (err) { + return next(err); + } + + topics.forEach(function(topic) { + if (topic) { + topicUrls.push({ + url: '/topic/' + topic.tid + '/' + encodeURIComponent(utils.slugify(topic.title)), + lastmodISO: utils.toISOString(topic.lastposttime), + changefreq: 'daily', + priority: '0.6' + }); + } }); - sitemap.obj.toXML(callback); + next(null, topicUrls); }); - }, - clearCache: function() { - if (sitemap.obj) { - sitemap.obj.clearCache(); - } } - }; + }, function(err, data) { + if (!err) { + returnUrls = data.categoryUrls.concat(data.topicUrls); + } + + callback(err, returnUrls); + }); +}; + + +sitemap.clearCache = function() { + if (sitemap.obj) { + sitemap.obj.clearCache(); + } +}; module.exports = sitemap;