5 var env = require('gitter-web-env');
6 var nconf = env.config;
8 var persistence = require('gitter-web-persistence');
9 var fs = require('fs');
10 var BatchStream = require('batch-stream');
11 var through2Concurrent = require('through2-concurrent');
12 var basePath = nconf.get('web:basepath');
13 var sitemapLocation = nconf.get('sitemap:location');
15 var opts = require('yargs')
19 description: 'Where to write the sitemap files to'
24 description: 'What to call the sitemap (ie. the prefix)'
27 .alias('help', 'h').argv;
31 console.error(error.stack);
35 function roomToURL(room) {
36 return basePath + '/' + room.uri + '/archives';
39 function createSitemap(urls) {
41 xml.push('<?xml version="1.0" encoding="UTF-8"?>');
42 xml.push('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
43 urls.forEach(function(url) {
45 xml.push('<loc>' + url + '</loc>');
46 xml.push('<changefreq>daily</changefreq>');
49 xml.push('</urlset>');
50 return xml.join('\n');
53 function createSitemapIndex(urls) {
56 xml.push('<?xml version="1.0" encoding="UTF-8"?>');
58 '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' +
59 'xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" ' +
60 'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">'
63 urls.forEach(function(url) {
64 xml.push('<sitemap>');
65 xml.push('<loc>' + url + '</loc>');
66 xml.push('</sitemap>');
69 xml.push('</sitemapindex>');
70 return xml.join('\n');
77 $or: [{ noindex: { $exists: false } }, { noindex: false }]
79 var projection = { _id: 1, uri: 1 };
80 persistence.Troupe.find(query, projection)
84 .pipe(new BatchStream({ size: 50000 }))
86 through2Concurrent.obj({ maxConcurrency: 10 }, function(rooms, enc, callback) {
88 sitemapURLs.push(sitemapLocation.replace('.xml', '-' + pageNum + '.xml'));
89 var sitemap = createSitemap(rooms.map(roomToURL));
90 fs.writeFile(opts.tempdir + '/' + opts.name + '-' + pageNum + '.xml', sitemap, callback);
93 .on('data', function() {})
94 .on('end', function() {
95 var indexData = createSitemapIndex(sitemapURLs);
96 fs.writeFile(opts.tempdir + '/' + opts.name + '.xml', indexData, function() {