2012-09-18 23:36:20 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2012-10-09 00:06:12 +08:00
|
|
|
var path = require('path');
|
2012-09-18 23:36:20 +08:00
|
|
|
|
2012-10-09 00:06:12 +08:00
|
|
|
// Reset all styles in the store
|
|
|
|
var grainstore = require('../node_modules/grainstore/lib/grainstore');
|
|
|
|
var mapnik = require('mapnik');
|
|
|
|
var redis = require('redis');
|
2012-09-18 23:36:20 +08:00
|
|
|
|
2012-10-09 00:06:12 +08:00
|
|
|
function usage(me, exitcode) {
|
|
|
|
console.log("Usage: " + me + " [--convert] [<target_mapnik_version>]");
|
|
|
|
process.exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
var doConvert = false;
|
|
|
|
var MAPNIK_VERSION;
|
|
|
|
|
|
|
|
var node_path = process.argv.shift();
|
|
|
|
var script_path = process.argv.shift();
|
|
|
|
var me = path.basename(script_path);
|
|
|
|
var arg;
|
|
|
|
while ( arg = process.argv.shift() ) {
|
|
|
|
if ( arg == '--convert' ) {
|
|
|
|
doConvert = true;
|
|
|
|
} else if ( ! MAPNIK_VERSION ) {
|
|
|
|
MAPNIK_VERSION = arg;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
usage(me, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! MAPNIK_VERSION ) {
|
|
|
|
MAPNIK_VERSION = mapnik.versions.mapnik;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log( (doConvert ? "Converting" : "Resetting" ) + ' all styles to target ' + MAPNIK_VERSION);
|
|
|
|
|
|
|
|
var REDIS_PORT = 6379; // TODO: make a command line parameter
|
2012-09-18 23:36:20 +08:00
|
|
|
|
|
|
|
var dbnum = 0;
|
|
|
|
|
2012-10-09 00:06:12 +08:00
|
|
|
var mml_store = new grainstore.MMLStore({port:REDIS_PORT}, {mapnik_version:MAPNIK_VERSION});
|
|
|
|
|
|
|
|
var failures = [];
|
|
|
|
|
2012-09-18 23:36:20 +08:00
|
|
|
var client = redis.createClient(REDIS_PORT, 'localhost');
|
|
|
|
client.on('connect', function() {
|
|
|
|
client.select(dbnum);
|
|
|
|
client.keys('map_style|*', function(err, matches) {
|
2012-09-21 18:58:34 +08:00
|
|
|
|
|
|
|
processNext = function() {
|
2012-10-09 00:06:12 +08:00
|
|
|
if ( ! matches.length ) process.exit(failures.length);
|
2012-09-21 18:58:34 +08:00
|
|
|
var k = matches.shift();
|
2012-10-09 00:06:12 +08:00
|
|
|
|
|
|
|
if ( /map_style\|.*\|.*\|/.test(k) ) {
|
|
|
|
//console.warn("Key " + k + " is EXTENDED, skipping");
|
|
|
|
processNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
var params = RegExp(/map_style\|(.*)\|(.*)/).exec(k);
|
|
|
|
var db = params[1];
|
|
|
|
var tab = params[2];
|
|
|
|
var out = 'map_style|' + db + '|' + tab + ': ';
|
|
|
|
|
|
|
|
var mml_builder = mml_store.mml_builder({dbname:db, table:tab},
|
|
|
|
function(err, payload) {
|
|
|
|
|
|
|
|
if ( err ) { console.warn(out + err.message); failures.push(k); processNext(); }
|
|
|
|
else {
|
|
|
|
mml_builder.resetStyle(function(err, data) {
|
|
|
|
if ( err ) { console.warn(out + err.message); failures.push(k); }
|
|
|
|
else console.log(out + 'OK' + ( doConvert ? ' (converted)' : '' ));
|
|
|
|
processNext();
|
|
|
|
}, doConvert);
|
|
|
|
}
|
2012-09-18 23:36:20 +08:00
|
|
|
});
|
2012-10-09 00:06:12 +08:00
|
|
|
|
|
|
|
};
|
2012-09-21 18:58:34 +08:00
|
|
|
|
|
|
|
processNext();
|
|
|
|
|
2012-09-18 23:36:20 +08:00
|
|
|
});
|
2012-10-09 00:06:12 +08:00
|
|
|
|
2012-09-18 23:36:20 +08:00
|
|
|
});
|