Skip to content

Commit

Permalink
Make env.switchToConfig promise returning
Browse files Browse the repository at this point in the history
Change-Id: Ic4e30a5597324987486e871245e468476d677beb
  • Loading branch information
arlolra committed Apr 22, 2015
1 parent b9fd496 commit 915ea3f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 79 deletions.
54 changes: 30 additions & 24 deletions lib/mediawiki.ApiRequest.js
Expand Up @@ -25,6 +25,25 @@ var logAPIWarnings = function( request, data ) {
}
};

// Helper to return a promise returning function for the result of an
// (Ctor-type) ApiRequest.
var promiseFor = function(Ctor) {
return function() {
var args = Array.prototype.slice.call(arguments);
return new Promise(function(resolve, reject) {
var req = Object.create(Ctor.prototype);
Ctor.apply(req, args);
req.once('src', function(err, src) {
if (err) {
reject(err);
} else {
resolve(src);
}
});
});
};
};

/**
* @class
* @extends Error
Expand Down Expand Up @@ -351,18 +370,13 @@ TemplateRequest.prototype._handleJSON = function( error, data ) {
this._processListeners( null, metadata );
};

// Promise to set page src info
// Function which returns a promise for the result of a template request.
TemplateRequest.promise = promiseFor(TemplateRequest);

// Function which returns a promise to set page src info.
TemplateRequest.setPageSrcInfo = function(env, target, oldid) {
return new Promise(function(resolve, reject) {
var tpr = new TemplateRequest(env, target, oldid);
tpr.once('src', function(err, srcAndMetadata) {
if (err) {
reject(err);
} else {
env.setPageSrcInfo(srcAndMetadata);
resolve();
}
});
return TemplateRequest.promise(env, target, oldid).then(function(src) {
env.setPageSrcInfo(src);
});
};

Expand Down Expand Up @@ -713,19 +727,8 @@ ParsoidCacheRequest.prototype._handleBody = function( error, body ) {
this._processListeners( error, body );
};

// Promise to return the cache request
ParsoidCacheRequest.promise = function(env, title, oldid, options) {
return new Promise(function(resolve, reject) {
var cacheRequest = new ParsoidCacheRequest(env, title, oldid, options);
cacheRequest.once('src', function(err, src) {
if (err) {
reject(err);
} else {
resolve(src);
}
});
});
};
// Function which returns a promise for the result of a cache request.
ParsoidCacheRequest.promise = promiseFor(ParsoidCacheRequest);

/**
* @class
Expand Down Expand Up @@ -822,6 +825,9 @@ ConfigRequest.prototype._handleJSON = function( error, data ) {
this._processListeners( error, resultConf );
};

// Function which returns a promise for the result of a config request.
ConfigRequest.promise = promiseFor(ConfigRequest);

/**
* @class
* @extends ApiRequest
Expand Down
105 changes: 50 additions & 55 deletions lib/mediawiki.parser.environment.js
Expand Up @@ -229,27 +229,21 @@ MWParserEnvironment.prototype.setVariable = function( varname, value, options )
*
* @param {ParsoidConfig/null} parsoidConfig
* @param {WikiConfig/null} wikiConfig
* @param {string} prefix The interwiki prefix that corresponds to the wiki we should use
* @param {string} pageName
* @param {Object} options
* @param {Function} cb
* @param {Error} cb.err
* @param {MWParserEnvironment} cb.env The finished environment object
*/
MWParserEnvironment.getParserEnv = function( parsoidConfig, wikiConfig, options, cb ) {
MWParserEnvironment.getParserEnv = function(parsoidConfig, wikiConfig, options, cb) {
options = options || {};
cb = JSUtils.mkPromised(cb);

var env = new MWParserEnvironment(parsoidConfig, wikiConfig, options);

// Get that wiki's config
env.switchToConfig(options.prefix, function( err ) {
if ( !err && !options.pageName ) {
env.initializeForPageName( env.conf.wiki.mainpage, true );
return env.switchToConfig(options.prefix).then(function() {
if (!options.pageName) {
env.initializeForPageName(env.conf.wiki.mainpage, true);
}
cb( err, env );
});

return cb.promise;
return env;
}).nodify(cb);
};

/**
Expand Down Expand Up @@ -279,52 +273,53 @@ MWParserEnvironment.prototype.getAPIProxy = function( prefix ) {
* @param {Function} cb
* @param {Error} cb.err
*/
MWParserEnvironment.prototype.switchToConfig = function( prefix, cb ) {
function setupWikiConfig(env, apiURI, error, resultConf) {
if ( error === null ) {
env.conf.wiki = new WikiConfig( env, resultConf, prefix, apiURI, env.getAPIProxy(prefix) );
env.confCache[prefix] = env.conf.wiki;
MWParserEnvironment.prototype.switchToConfig = function(prefix, cb) {
var env = this;
var nothingToDo = {}; // unique marker value
var parsoid = env.conf.parsoid;
return new Promise(function(resolve, reject) {
if (!prefix) {
return reject(new Error('Wiki prefix not provided'));
}
cb( error );
}

if (!prefix) {
this.log("error", "No prefix provided!");
cb(new Error("Wiki prefix not provided"));
return;
}

var uri = this.conf.parsoid.interwikiMap.get(prefix);
if (!uri) {
// SSS: Ugh! Looks like parser tests use a prefix
// that is not part of the interwikiMap -- so we
// cannot crash with an error. Hence defaulting
// to enwiki api which is quite odd. Does the
// interwikiMap need updating or is this use-case
// valid outside of parserTests??
this.log("error", "Did not find api uri for " + prefix + "; defaulting to enwiki");
uri = this.conf.parsoid.interwikiMap.get("enwiki");
}

this.conf.parsoid.apiURI = uri;

if ( this.confCache[prefix] ) {
this.conf.wiki = this.confCache[prefix];
cb( null );
} else if ( this.conf.parsoid.fetchConfig ) {
var confRequest = new ConfigRequest( uri, this, this.getAPIProxy(prefix) );
confRequest.once('src', setupWikiConfig.bind(null, this, uri));
} else {
// Load the config from cached config on disk
var localConfigFile = './baseconfig/' + prefix + '.json';
var localConfig = require(localConfigFile);

if (localConfig && localConfig.query) {
setupWikiConfig(this, uri, null, localConfig.query);
var uri = parsoid.interwikiMap.get(prefix);
if (!uri) {
// SSS: Ugh! Looks like parser tests use a prefix
// that is not part of the interwikiMap -- so we
// cannot crash with an error. Hence defaulting
// to enwiki api which is quite odd. Does the
// interwikiMap need updating or is this use-case
// valid outside of parserTests??
env.log('error', 'Did not find api uri for ' +
prefix + '; defaulting to enwiki');
uri = parsoid.interwikiMap.get('enwiki');
}
parsoid.apiURI = uri;

if (env.confCache[prefix]) {
env.conf.wiki = env.confCache[prefix];
return resolve(nothingToDo);
} else if (env.conf.parsoid.fetchConfig) {
ConfigRequest
.promise(uri, env, env.getAPIProxy(prefix))
.then(resolve, reject);
} else {
cb(new Error("Could not read valid config from file: " + localConfigFile));
// Load the config from cached config on disk
var localConfigFile = './baseconfig/' + prefix + '.json';
var localConfig = require(localConfigFile);
if (localConfig && localConfig.query) {
return resolve(localConfig.query);
} else {
return reject(new Error('Could not read valid config from' +
' file: ' + localConfigFile));
}
}
}
}).then(function(resultConf) {
if (resultConf === nothingToDo) { return; }
env.conf.wiki = new WikiConfig(env, resultConf, prefix, parsoid.apiURI,
env.getAPIProxy(prefix));
env.confCache[prefix] = env.conf.wiki;
}).nodify(cb);
};

// XXX: move to Title!
Expand Down

0 comments on commit 915ea3f

Please sign in to comment.