2012-01-06 14:30:55 +00:00
|
|
|
/**
|
2012-01-09 19:33:49 +00:00
|
|
|
* Template and template argument handling, first cut.
|
|
|
|
*
|
|
|
|
* AsyncTokenTransformManager objects provide preprocessor-frame-like
|
|
|
|
* functionality once template args etc are fully expanded, and isolate
|
2012-01-20 00:49:27 +00:00
|
|
|
* individual transforms from concurrency issues. Template expansion is
|
|
|
|
* controlled using a tplExpandData structure created independently for each
|
|
|
|
* handled template tag.
|
2012-01-06 14:30:55 +00:00
|
|
|
*
|
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
2012-01-06 17:19:14 +00:00
|
|
|
* @author Brion Vibber <brion@wikimedia.org>
|
2012-01-06 14:30:55 +00:00
|
|
|
*/
|
2012-01-11 19:48:49 +00:00
|
|
|
var $ = require('jquery'),
|
2012-01-18 19:38:32 +00:00
|
|
|
request = require('request'),
|
2012-01-20 02:36:18 +00:00
|
|
|
events = require('events'),
|
2012-01-18 19:38:32 +00:00
|
|
|
qs = require('querystring'),
|
2012-01-21 20:38:13 +00:00
|
|
|
ParserFunctions = require('./ext.core.ParserFunctions.js').ParserFunctions,
|
2012-01-18 19:38:32 +00:00
|
|
|
AttributeTransformManager = require('./mediawiki.TokenTransformManager.js')
|
2012-02-01 16:30:43 +00:00
|
|
|
.AttributeTransformManager,
|
|
|
|
defines = require('./mediawiki.parser.defines.js');
|
2012-01-06 17:19:14 +00:00
|
|
|
|
2012-01-06 14:30:55 +00:00
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
function TemplateHandler ( manager ) {
|
2012-01-06 14:30:55 +00:00
|
|
|
this.reset();
|
2012-01-11 19:48:49 +00:00
|
|
|
this.register( manager );
|
2012-01-21 20:38:13 +00:00
|
|
|
this.parserFunctions = new ParserFunctions( manager );
|
2012-01-06 14:30:55 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 20:01:21 +00:00
|
|
|
TemplateHandler.prototype.reset = function ( token ) {
|
|
|
|
return {token: token};
|
2012-01-06 14:30:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// constants
|
|
|
|
TemplateHandler.prototype.rank = 1.1;
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
TemplateHandler.prototype.register = function ( manager ) {
|
|
|
|
this.manager = manager;
|
2012-01-06 14:30:55 +00:00
|
|
|
// Register for template and templatearg tag tokens
|
2012-01-09 17:49:16 +00:00
|
|
|
manager.addTransform( this.onTemplate.bind(this),
|
2012-01-06 14:30:55 +00:00
|
|
|
this.rank, 'tag', 'template' );
|
2012-01-17 22:29:26 +00:00
|
|
|
|
|
|
|
// Template argument expansion
|
2012-01-09 17:49:16 +00:00
|
|
|
manager.addTransform( this.onTemplateArg.bind(this),
|
2012-01-06 14:30:55 +00:00
|
|
|
this.rank, 'tag', 'templatearg' );
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main template token handler
|
|
|
|
*
|
|
|
|
* Expands target and arguments (both keys and values) and either directly
|
|
|
|
* calls or sets up the callback to _expandTemplate, which then fetches and
|
|
|
|
* processes the template.
|
|
|
|
*/
|
2012-02-07 11:53:29 +00:00
|
|
|
TemplateHandler.prototype.onTemplate = function ( token, frame, cb ) {
|
2012-01-17 23:18:33 +00:00
|
|
|
//console.log('onTemplate! ' + JSON.stringify( token, null, 2 ) +
|
|
|
|
// ' args: ' + JSON.stringify( this.manager.args ));
|
2012-01-13 18:48:25 +00:00
|
|
|
|
2012-01-06 14:30:55 +00:00
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
// create a new temporary frame for argument and title expansions
|
2012-01-20 00:49:27 +00:00
|
|
|
var tplExpandData = {
|
2012-01-06 14:30:55 +00:00
|
|
|
args: {},
|
2012-01-11 19:48:49 +00:00
|
|
|
manager: this.manager,
|
2012-01-10 01:09:50 +00:00
|
|
|
cb: cb,
|
2012-01-11 00:05:51 +00:00
|
|
|
origToken: token,
|
2012-01-19 23:43:39 +00:00
|
|
|
resultTokens: [],
|
|
|
|
attribsAsync: true,
|
|
|
|
overallAsync: false,
|
|
|
|
expandDone: false
|
2012-01-06 14:30:55 +00:00
|
|
|
},
|
2012-01-10 01:09:50 +00:00
|
|
|
transformCB,
|
2012-01-06 14:30:55 +00:00
|
|
|
i = 0,
|
2012-02-01 16:30:43 +00:00
|
|
|
res;
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2012-02-01 16:30:43 +00:00
|
|
|
var attributes = [ {k: [''] , v: token.target} ]
|
2012-01-17 22:29:26 +00:00
|
|
|
.concat( this._nameArgs( token.orderedArgs ) );
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2012-01-21 03:03:33 +00:00
|
|
|
//this.manager.env.dp( 'before AttributeTransformManager: ' +
|
|
|
|
// JSON.stringify( attributes, null, 2 ) );
|
2012-01-13 18:48:25 +00:00
|
|
|
new AttributeTransformManager(
|
|
|
|
this.manager,
|
2012-01-20 00:49:27 +00:00
|
|
|
this._returnAttributes.bind( this, tplExpandData )
|
2012-01-11 00:05:51 +00:00
|
|
|
).process( attributes );
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2012-01-13 18:48:25 +00:00
|
|
|
// Unblock finish
|
2012-01-20 00:49:27 +00:00
|
|
|
if ( ! tplExpandData.attribsAsync ) {
|
2012-01-19 23:43:39 +00:00
|
|
|
// Attributes were transformed synchronously
|
2012-01-20 22:02:23 +00:00
|
|
|
this.manager.env.dp (
|
|
|
|
'sync attribs for ' + JSON.stringify( tplExpandData.target ),
|
|
|
|
tplExpandData.expandedArgs
|
|
|
|
);
|
2012-01-18 19:38:32 +00:00
|
|
|
// All attributes are fully expanded synchronously (no IO was needed)
|
2012-01-20 00:49:27 +00:00
|
|
|
return this._expandTemplate ( tplExpandData );
|
2012-01-06 14:30:55 +00:00
|
|
|
} else {
|
2012-01-19 23:43:39 +00:00
|
|
|
// Async attribute expansion is going on
|
|
|
|
this.manager.env.dp( 'async return for ' + JSON.stringify( token ));
|
2012-01-20 00:49:27 +00:00
|
|
|
tplExpandData.overallAsync = true;
|
2012-01-06 14:30:55 +00:00
|
|
|
return { async: true };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-19 23:43:39 +00:00
|
|
|
/**
|
|
|
|
* Create positional (number) keys for arguments without explicit keys
|
|
|
|
*/
|
2012-01-17 22:29:26 +00:00
|
|
|
TemplateHandler.prototype._nameArgs = function ( orderedArgs ) {
|
|
|
|
var n = 1,
|
|
|
|
out = [];
|
|
|
|
for ( var i = 0, l = orderedArgs.length; i < l; i++ ) {
|
2012-01-21 03:03:33 +00:00
|
|
|
// FIXME: Also check for whitespace-only named args!
|
2012-02-01 16:30:43 +00:00
|
|
|
if ( ! orderedArgs[i].k.length ) {
|
|
|
|
out.push( {k: [ n.toString() ], v: orderedArgs[i].v } );
|
2012-01-17 22:29:26 +00:00
|
|
|
n++;
|
|
|
|
} else {
|
|
|
|
out.push( orderedArgs[i] );
|
|
|
|
}
|
|
|
|
}
|
2012-01-20 22:02:23 +00:00
|
|
|
this.manager.env.dp( '_nameArgs: ' + JSON.stringify( out ) );
|
2012-01-17 22:29:26 +00:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2012-01-19 23:43:39 +00:00
|
|
|
/**
|
|
|
|
* Callback for argument (including target) expansion in AttributeTransformManager
|
|
|
|
*/
|
2012-01-20 00:49:27 +00:00
|
|
|
TemplateHandler.prototype._returnAttributes = function ( tplExpandData,
|
2012-01-18 19:38:32 +00:00
|
|
|
attributes )
|
|
|
|
{
|
2012-01-19 23:43:39 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler._returnAttributes: ' + JSON.stringify(attributes) );
|
2012-01-11 00:05:51 +00:00
|
|
|
// Remove the target from the attributes
|
2012-01-20 00:49:27 +00:00
|
|
|
tplExpandData.attribsAsync = false;
|
2012-02-01 16:30:43 +00:00
|
|
|
tplExpandData.target = attributes[0].v;
|
2012-01-11 00:05:51 +00:00
|
|
|
attributes.shift();
|
2012-01-20 00:49:27 +00:00
|
|
|
tplExpandData.expandedArgs = attributes;
|
|
|
|
if ( tplExpandData.overallAsync ) {
|
|
|
|
this._expandTemplate ( tplExpandData );
|
2012-01-10 01:09:50 +00:00
|
|
|
}
|
2012-01-17 22:29:26 +00:00
|
|
|
};
|
2012-01-06 14:30:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch, tokenize and token-transform a template after all arguments and the
|
2012-01-19 23:43:39 +00:00
|
|
|
* target were expanded.
|
2012-01-06 14:30:55 +00:00
|
|
|
*/
|
2012-01-20 00:49:27 +00:00
|
|
|
TemplateHandler.prototype._expandTemplate = function ( tplExpandData ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log('TemplateHandler.expandTemplate: ' +
|
2012-01-20 00:49:27 +00:00
|
|
|
// JSON.stringify( tplExpandData, null, 2 ) );
|
2012-01-22 01:27:22 +00:00
|
|
|
var res;
|
2012-01-20 00:49:27 +00:00
|
|
|
|
2012-01-19 23:43:39 +00:00
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
if ( ! tplExpandData.target ) {
|
2012-01-19 23:43:39 +00:00
|
|
|
this.manager.env.dp( 'No target! ' +
|
2012-01-20 00:49:27 +00:00
|
|
|
JSON.stringify( tplExpandData, null, 2 ) );
|
2012-01-19 23:43:39 +00:00
|
|
|
console.trace();
|
|
|
|
}
|
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
// TODO:
|
|
|
|
// check for 'subst:'
|
|
|
|
// check for variable magic names
|
|
|
|
// check for msg, msgnw, raw magics
|
|
|
|
// check for parser functions
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
// First, check the target for loops
|
2012-01-31 16:50:16 +00:00
|
|
|
var target = this.manager.env.tokensToString( tplExpandData.target ).trim();
|
2012-01-22 07:07:16 +00:00
|
|
|
|
2012-01-21 20:38:13 +00:00
|
|
|
var args = this.manager.env.KVtoHash( tplExpandData.expandedArgs );
|
|
|
|
|
|
|
|
this.manager.env.dp( 'argHash: ', args );
|
|
|
|
|
2012-01-31 16:50:16 +00:00
|
|
|
var prefix = target.split(':', 1)[0].toLowerCase().trim();
|
2012-01-21 20:38:13 +00:00
|
|
|
if ( prefix && 'pf_' + prefix in this.parserFunctions ) {
|
|
|
|
var funcArg = target.substr( prefix.length + 1 );
|
2012-02-06 19:15:44 +00:00
|
|
|
this.manager.env.tp( 'func prefix: ' + prefix +
|
|
|
|
' args=' + JSON.stringify( tplExpandData.expandedArgs, null, 2) +
|
|
|
|
' funcArg=' + funcArg);
|
2012-01-31 16:50:16 +00:00
|
|
|
//this.manager.env.dp( 'entering prefix', funcArg, args );
|
2012-01-22 01:27:22 +00:00
|
|
|
res = this.parserFunctions[ 'pf_' + prefix ]( funcArg,
|
2012-01-22 07:07:16 +00:00
|
|
|
tplExpandData.expandedArgs, args );
|
2012-01-21 20:38:13 +00:00
|
|
|
|
|
|
|
// XXX: support async parser functions!
|
|
|
|
if ( tplExpandData.overallAsync ) {
|
|
|
|
this.manager.env.dp( 'TemplateHandler._expandTemplate: calling back ' +
|
|
|
|
'after parser func ' + prefix + ' with res:' + JSON.stringify( res ) );
|
|
|
|
return tplExpandData.cb( res, false );
|
|
|
|
} else {
|
|
|
|
this.manager.env.dp( 'TemplateHandler._expandTemplate: sync return ' +
|
|
|
|
'after parser func ' + prefix + ' with res:' + JSON.stringify( res ) );
|
|
|
|
return { tokens: res };
|
|
|
|
//data.reset();
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 16:50:16 +00:00
|
|
|
this.manager.env.tp( 'template target: ' + target );
|
2012-01-21 20:38:13 +00:00
|
|
|
|
2012-01-22 07:07:16 +00:00
|
|
|
// now normalize the target before template processing
|
|
|
|
target = this.manager.env.normalizeTitle( target );
|
|
|
|
|
2012-01-22 19:32:28 +00:00
|
|
|
var checkRes = this.manager.loopAndDepthCheck.check( target, this.manager.env.maxDepth );
|
2012-01-19 23:43:39 +00:00
|
|
|
if( checkRes ) {
|
2012-01-22 01:27:22 +00:00
|
|
|
// Loop detected or depth limit exceeded, abort!
|
|
|
|
res = [
|
2012-02-01 16:30:43 +00:00
|
|
|
checkRes,
|
|
|
|
new TagTk( 'a', [{k: 'href', v: target}] ),
|
|
|
|
target,
|
|
|
|
new EndTagTk( 'a' )
|
2012-01-22 01:27:22 +00:00
|
|
|
];
|
|
|
|
if ( tplExpandData.overallAsync ) {
|
|
|
|
return tplExpandData.cb( res, false );
|
|
|
|
} else {
|
|
|
|
return { tokens: res };
|
|
|
|
}
|
2012-01-14 00:58:20 +00:00
|
|
|
}
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
// Get a nested transformation pipeline for the input type. The input
|
|
|
|
// pipeline includes the tokenizer, synchronous stage-1 transforms for
|
|
|
|
// 'text/wiki' input and asynchronous stage-2 transforms).
|
2012-01-17 23:18:33 +00:00
|
|
|
var inputPipeline = this.manager.newChildPipeline(
|
2012-01-14 00:58:20 +00:00
|
|
|
this.manager.inputType || 'text/wiki',
|
2012-01-21 20:38:13 +00:00
|
|
|
args,
|
2012-01-20 00:49:27 +00:00
|
|
|
tplExpandData.target
|
2012-01-11 19:48:49 +00:00
|
|
|
);
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
// Hook up the inputPipeline output events to our handlers
|
|
|
|
inputPipeline.addListener( 'chunk', this._onChunk.bind ( this, tplExpandData ) );
|
|
|
|
inputPipeline.addListener( 'end', this._onEnd.bind ( this, tplExpandData ) );
|
2012-01-10 01:09:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Resolve a possibly relative link
|
2012-01-13 18:48:25 +00:00
|
|
|
var templateName = this.manager.env.resolveTitle(
|
2012-01-14 00:58:20 +00:00
|
|
|
target,
|
2012-01-13 18:48:25 +00:00
|
|
|
'Template'
|
|
|
|
);
|
2012-01-20 00:49:27 +00:00
|
|
|
|
|
|
|
// XXX: notes from brion's mediawiki.parser.environment
|
|
|
|
// resolve template name
|
|
|
|
// load template w/ canonical name
|
|
|
|
// load template w/ variant names (language variants)
|
|
|
|
|
|
|
|
// For now, just fetch the template and pass the callback for further
|
|
|
|
// processing along.
|
2012-01-19 23:43:39 +00:00
|
|
|
this._fetchTemplateAndTitle(
|
|
|
|
templateName,
|
|
|
|
this._processTemplateAndTitle.bind( this, inputPipeline ),
|
2012-01-20 00:49:27 +00:00
|
|
|
tplExpandData
|
2012-01-19 23:43:39 +00:00
|
|
|
);
|
2012-01-09 19:33:49 +00:00
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
// If nothing was async so far and the template source was retrieved and
|
|
|
|
// fully processed without async requests (using the cache), then
|
|
|
|
// expandDone is set to true in our _onEnd handler.
|
|
|
|
if ( tplExpandData.overallAsync ||
|
|
|
|
! tplExpandData.expandDone ) {
|
|
|
|
tplExpandData.overallAsync = true;
|
2012-01-19 23:43:39 +00:00
|
|
|
this.manager.env.dp( 'Async return from _expandTemplate for ' +
|
2012-01-20 00:49:27 +00:00
|
|
|
JSON.stringify ( tplExpandData.target ) );
|
2012-01-18 23:46:01 +00:00
|
|
|
return { async: true };
|
2012-01-13 18:48:25 +00:00
|
|
|
} else {
|
2012-01-19 23:43:39 +00:00
|
|
|
this.manager.env.dp( 'Sync return from _expandTemplate for ' +
|
2012-01-20 00:49:27 +00:00
|
|
|
JSON.stringify( tplExpandData.target ) + ' : ' +
|
|
|
|
JSON.stringify( tplExpandData.result )
|
2012-01-19 23:43:39 +00:00
|
|
|
);
|
2012-01-20 00:49:27 +00:00
|
|
|
return tplExpandData.result;
|
2012-01-13 18:48:25 +00:00
|
|
|
}
|
2012-01-06 14:30:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
/**
|
2012-01-19 23:43:39 +00:00
|
|
|
* Handle chunk emitted from the input pipeline after feeding it a template
|
2012-01-10 01:09:50 +00:00
|
|
|
*/
|
2012-01-20 00:49:27 +00:00
|
|
|
TemplateHandler.prototype._onChunk = function( tplExpandData, chunk ) {
|
2012-01-10 01:09:50 +00:00
|
|
|
// We encapsulate the output by default, so collect tokens here.
|
2012-01-19 23:43:39 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler._onChunk' + JSON.stringify( chunk ) );
|
2012-01-20 00:49:27 +00:00
|
|
|
tplExpandData.resultTokens = tplExpandData.resultTokens.concat( chunk );
|
2012-01-10 01:09:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-01-19 23:43:39 +00:00
|
|
|
* Handle the end event emitted by the parser pipeline after fully processing
|
|
|
|
* the template source.
|
2012-01-10 01:09:50 +00:00
|
|
|
*/
|
2012-01-20 00:49:27 +00:00
|
|
|
TemplateHandler.prototype._onEnd = function( tplExpandData, token ) {
|
|
|
|
this.manager.env.dp( 'TemplateHandler._onEnd' + JSON.stringify( tplExpandData.resultTokens ) );
|
|
|
|
tplExpandData.expandDone = true;
|
|
|
|
var res = tplExpandData.resultTokens;
|
2012-01-17 20:01:21 +00:00
|
|
|
// Remove 'end' token from end
|
|
|
|
if ( res.length && res[res.length - 1].type === 'END' ) {
|
2012-01-21 03:03:33 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler, stripping end ' );
|
2012-01-17 20:01:21 +00:00
|
|
|
res.pop();
|
|
|
|
}
|
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
// Could also encapsulate the template tokens here, if that turns out
|
|
|
|
// better for the editor.
|
|
|
|
|
2012-01-13 18:48:25 +00:00
|
|
|
//console.log( 'TemplateHandler._onEnd: ' + JSON.stringify( res, null, 2 ) );
|
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
if ( tplExpandData.overallAsync ) {
|
2012-01-19 23:43:39 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler._onEnd: calling back with res:' +
|
|
|
|
JSON.stringify( res ) );
|
2012-01-20 00:49:27 +00:00
|
|
|
tplExpandData.cb( res, false );
|
2012-01-13 18:48:25 +00:00
|
|
|
} else {
|
2012-01-19 23:43:39 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler._onEnd: synchronous return!' );
|
2012-01-20 00:49:27 +00:00
|
|
|
tplExpandData.result = { tokens: res };
|
2012-01-19 23:43:39 +00:00
|
|
|
//data.reset();
|
2012-01-13 18:48:25 +00:00
|
|
|
}
|
2012-01-10 01:09:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a fetched template source
|
|
|
|
*/
|
2012-01-17 23:18:33 +00:00
|
|
|
TemplateHandler.prototype._processTemplateAndTitle = function( pipeline, src, title ) {
|
2012-01-10 01:09:50 +00:00
|
|
|
// Feed the pipeline. XXX: Support different formats.
|
2012-01-19 23:43:39 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler._processTemplateAndTitle: ' + src );
|
2012-01-17 23:18:33 +00:00
|
|
|
pipeline.process ( src );
|
2012-01-10 01:09:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-06 17:19:14 +00:00
|
|
|
/**
|
|
|
|
* Fetch a template
|
|
|
|
*/
|
2012-01-20 00:49:27 +00:00
|
|
|
TemplateHandler.prototype._fetchTemplateAndTitle = function ( title, callback, tplExpandData ) {
|
2012-01-06 17:19:14 +00:00
|
|
|
// @fixme normalize name?
|
2012-01-18 19:38:32 +00:00
|
|
|
var self = this;
|
2012-01-20 18:47:17 +00:00
|
|
|
if ( title in this.manager.env.pageCache ) {
|
|
|
|
// Unwind the stack
|
2012-01-20 00:49:27 +00:00
|
|
|
process.nextTick(
|
|
|
|
function () {
|
2012-01-21 20:38:13 +00:00
|
|
|
callback( self.manager.env.pageCache[title], title );
|
2012-01-20 00:49:27 +00:00
|
|
|
}
|
|
|
|
);
|
2012-01-18 19:38:32 +00:00
|
|
|
} else if ( ! this.manager.env.fetchTemplates ) {
|
2012-01-22 07:07:16 +00:00
|
|
|
callback( 'Warning: Page/template fetching disabled, and no cache for ' +
|
|
|
|
title, title );
|
2012-01-06 17:19:14 +00:00
|
|
|
} else {
|
2012-01-20 02:36:18 +00:00
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
// We are about to start an async request for a template, so mark this
|
|
|
|
// template expansion as such.
|
|
|
|
tplExpandData.overallAsync = true;
|
2012-01-22 07:07:16 +00:00
|
|
|
this.manager.env.dp( 'Note: trying to fetch ' + title );
|
2012-01-18 19:38:32 +00:00
|
|
|
|
2012-01-20 02:36:18 +00:00
|
|
|
// Start a new request if none is outstanding
|
2012-01-21 20:38:13 +00:00
|
|
|
this.manager.env.dp( 'requestQueue: ', this.manager.env.requestQueue);
|
2012-01-20 02:36:18 +00:00
|
|
|
if ( this.manager.env.requestQueue[title] === undefined ) {
|
2012-01-31 16:50:16 +00:00
|
|
|
this.manager.env.tp( 'Note: Starting new request for ' + title );
|
2012-01-20 02:36:18 +00:00
|
|
|
this.manager.env.requestQueue[title] = new TemplateRequest( this.manager, title );
|
|
|
|
}
|
|
|
|
// Append a listener to the request
|
2012-01-31 16:50:16 +00:00
|
|
|
this.manager.env.requestQueue[title].once( 'src', callback );
|
2012-01-18 19:38:32 +00:00
|
|
|
|
2012-01-06 17:19:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-19 23:43:39 +00:00
|
|
|
/*********************** Template argument expansion *******************/
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Expand template arguments with tokens from the containing frame.
|
|
|
|
*/
|
2012-02-07 11:53:29 +00:00
|
|
|
TemplateHandler.prototype.onTemplateArg = function ( token, frame, cb ) {
|
2012-01-17 22:29:26 +00:00
|
|
|
|
2012-02-01 16:30:43 +00:00
|
|
|
var attributes = [{k: token.argname, v: token.defaultvalue}];
|
2012-01-17 22:29:26 +00:00
|
|
|
|
|
|
|
token.resultTokens = false;
|
|
|
|
|
2012-01-31 16:50:16 +00:00
|
|
|
new AttributeTransformManager (
|
2012-01-17 22:29:26 +00:00
|
|
|
this.manager,
|
|
|
|
this._returnArgAttributes.bind( this, token, cb, frame )
|
|
|
|
).process( attributes );
|
|
|
|
|
|
|
|
if ( token.resultTokens !== false ) {
|
|
|
|
// synchronous return
|
|
|
|
//console.log( 'synchronous attribute expand: ' + JSON.stringify( token.resultTokens ) );
|
|
|
|
|
|
|
|
return { tokens: token.resultTokens };
|
|
|
|
} else {
|
|
|
|
//console.log( 'asynchronous attribute expand: ' + JSON.stringify( token, null, 2 ) );
|
|
|
|
// asynchronous return
|
|
|
|
token.resultTokens = [];
|
|
|
|
return { async: true };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TemplateHandler.prototype._returnArgAttributes = function ( token, cb, frame, attributes ) {
|
|
|
|
//console.log( '_returnArgAttributes: ' + JSON.stringify( attributes ));
|
2012-02-01 16:30:43 +00:00
|
|
|
var argName = this.manager.env.tokensToString( attributes[0].k ).trim(),
|
|
|
|
defaultValue = attributes[0].v,
|
2012-01-17 22:29:26 +00:00
|
|
|
res;
|
2012-01-17 23:18:33 +00:00
|
|
|
if ( argName in this.manager.args ) {
|
2012-01-06 14:30:55 +00:00
|
|
|
// return tokens for argument
|
2012-01-17 22:29:26 +00:00
|
|
|
//console.log( 'templateArg found: ' + argName +
|
2012-01-17 23:18:33 +00:00
|
|
|
// ' vs. ' + JSON.stringify( this.manager.args ) );
|
|
|
|
res = this.manager.args[argName];
|
2012-01-06 14:30:55 +00:00
|
|
|
} else {
|
2012-01-21 20:38:13 +00:00
|
|
|
this.manager.env.dp( 'templateArg not found: ' + argName +
|
2012-01-20 22:02:23 +00:00
|
|
|
' vs. ' + JSON.stringify( this.manager.args ) );
|
2012-01-21 20:38:13 +00:00
|
|
|
if ( defaultValue.length ) {
|
2012-01-17 22:29:26 +00:00
|
|
|
res = defaultValue;
|
2012-01-06 17:19:14 +00:00
|
|
|
} else {
|
2012-02-01 16:30:43 +00:00
|
|
|
res = [ '{{{' + argName + '}}}' ];
|
2012-01-06 17:19:14 +00:00
|
|
|
}
|
2012-01-06 14:30:55 +00:00
|
|
|
}
|
2012-01-17 22:29:26 +00:00
|
|
|
if ( token.resultTokens !== false ) {
|
|
|
|
cb( res );
|
|
|
|
} else {
|
|
|
|
token.resultTokens = res;
|
|
|
|
}
|
2012-01-06 14:30:55 +00:00
|
|
|
};
|
|
|
|
|
2012-01-20 02:36:18 +00:00
|
|
|
|
|
|
|
/***************** Template fetch request helper class ********/
|
|
|
|
|
|
|
|
function TemplateRequest ( manager, title ) {
|
|
|
|
// Increase the number of maximum listeners a bit..
|
2012-01-22 23:21:53 +00:00
|
|
|
this.setMaxListeners( 10000 );
|
2012-01-20 02:36:18 +00:00
|
|
|
var self = this,
|
|
|
|
url = manager.env.wgScriptPath + '/api' +
|
|
|
|
manager.env.wgScriptExtension +
|
|
|
|
'?' +
|
|
|
|
qs.stringify( {
|
|
|
|
format: 'json',
|
|
|
|
action: 'query',
|
|
|
|
prop: 'revisions',
|
|
|
|
rvprop: 'content',
|
|
|
|
titles: title
|
|
|
|
} );
|
|
|
|
//'?format=json&action=query&prop=revisions&rvprop=content&titles=' + title;
|
|
|
|
|
|
|
|
request({
|
|
|
|
method: 'GET',
|
|
|
|
followRedirect: true,
|
|
|
|
url: url,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) ' +
|
|
|
|
'Gecko/20100101 Firefox/9.0.1 Iceweasel/9.0.1'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function (error, response, body) {
|
|
|
|
//console.log( 'response for ' + title + ' :' + body + ':' );
|
|
|
|
if(error) {
|
|
|
|
manager.env.dp(error);
|
2012-01-20 18:47:17 +00:00
|
|
|
self.emit('src', 'Page/template fetch failure for title ' + title, title);
|
|
|
|
} else if(response.statusCode == 200) {
|
2012-01-21 20:38:13 +00:00
|
|
|
var src = '',
|
|
|
|
data,
|
|
|
|
normalizedTitle;
|
2012-01-20 02:36:18 +00:00
|
|
|
try {
|
|
|
|
//console.log( 'body: ' + body );
|
2012-01-21 20:38:13 +00:00
|
|
|
data = JSON.parse( body );
|
2012-01-20 02:36:18 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.log( "Error: while parsing result. Error was: " );
|
|
|
|
console.log( e );
|
|
|
|
console.log( "Response that didn't parse was:");
|
|
|
|
console.log( "------------------------------------------\n" + body );
|
|
|
|
console.log( "------------------------------------------" );
|
|
|
|
}
|
|
|
|
try {
|
2012-01-21 20:38:13 +00:00
|
|
|
$.each( data.query.pages, function(i, page) {
|
2012-01-20 02:36:18 +00:00
|
|
|
if (page.revisions && page.revisions.length) {
|
|
|
|
src = page.revisions[0]['*'];
|
2012-01-21 20:38:13 +00:00
|
|
|
normalizeTitle = page.title;
|
2012-01-20 02:36:18 +00:00
|
|
|
}
|
|
|
|
});
|
2012-01-21 20:38:13 +00:00
|
|
|
} catch ( e2 ) {
|
2012-01-20 02:36:18 +00:00
|
|
|
console.log( 'Did not find page revisions in the returned body:' + body );
|
|
|
|
src = '';
|
|
|
|
}
|
|
|
|
//console.log( 'Page ' + title + ': got ' + src );
|
2012-01-31 16:50:16 +00:00
|
|
|
manager.env.tp( 'Retrieved ' + title );
|
2012-01-20 02:36:18 +00:00
|
|
|
manager.env.pageCache[title] = src;
|
|
|
|
self.emit( 'src', src, title );
|
|
|
|
}
|
2012-01-20 18:47:17 +00:00
|
|
|
// XXX: handle other status codes
|
|
|
|
|
|
|
|
// Remove self from request queue
|
2012-01-21 20:38:13 +00:00
|
|
|
manager.env.dp( 'trying to remove ' + title + ' from requestQueue' );
|
2012-01-20 18:47:17 +00:00
|
|
|
delete manager.env.requestQueue[title];
|
2012-01-21 20:38:13 +00:00
|
|
|
manager.env.dp( 'after deletion:', manager.env.requestQueue );
|
2012-01-20 02:36:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-01-21 20:38:13 +00:00
|
|
|
* XXX: The jQuery version does not quite work with node, but we keep
|
|
|
|
* it around for now.
|
2012-01-20 02:36:18 +00:00
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
data: {
|
|
|
|
format: 'json',
|
|
|
|
action: 'query',
|
|
|
|
prop: 'revisions',
|
|
|
|
rvprop: 'content',
|
|
|
|
titles: title
|
|
|
|
},
|
|
|
|
success: function(data, statusString, xhr) {
|
|
|
|
console.log( 'Page ' + title + ' success ' + JSON.stringify( data ) );
|
|
|
|
var src = null, title = null;
|
|
|
|
$.each(data.query.pages, function(i, page) {
|
|
|
|
if (page.revisions && page.revisions.length) {
|
|
|
|
src = page.revisions[0]['*'];
|
|
|
|
title = page.title;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (typeof src !== 'string') {
|
|
|
|
console.log( 'Page ' + title + 'not found! Got ' + src );
|
|
|
|
callback( 'Page ' + title + ' not found' );
|
|
|
|
} else {
|
|
|
|
// Add to cache
|
|
|
|
console.log( 'Page ' + title + ': got ' + src );
|
|
|
|
this.manager.env.pageCache[title] = src;
|
|
|
|
callback(src, title);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function(xhr, msg, err) {
|
|
|
|
console.log( 'Page/template fetch failure for title ' +
|
|
|
|
title + ', url=' + url + JSON.stringify(xhr) + ', err=' + err );
|
|
|
|
callback('Page/template fetch failure for title ' + title);
|
|
|
|
},
|
|
|
|
dataType: 'json',
|
|
|
|
cache: false, // @fixme caching, versions etc?
|
|
|
|
crossDomain: true
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Inherit from EventEmitter
|
|
|
|
TemplateRequest.prototype = new events.EventEmitter();
|
|
|
|
TemplateHandler.prototype.constructor = TemplateRequest;
|
|
|
|
|
|
|
|
|
2012-01-06 14:30:55 +00:00
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.TemplateHandler = TemplateHandler;
|
|
|
|
}
|