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-02-14 20:56:14 +00:00
|
|
|
//console.warn('onTemplate! ' + JSON.stringify( token, null, 2 ) +
|
2012-01-17 23:18:33 +00:00
|
|
|
// ' 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: [],
|
2012-02-09 13:44:20 +00:00
|
|
|
attribsAsync: true,
|
2012-01-19 23:43:39 +00:00
|
|
|
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-09 16:36:29 +00:00
|
|
|
var attributes = [token.attribs.shift()];
|
|
|
|
if( token.attribs.length ) {
|
|
|
|
attributes = attributes.concat( this._nameArgs( token.attribs ) );
|
|
|
|
}
|
2012-02-09 13:44:20 +00:00
|
|
|
|
|
|
|
this.manager.env.dp( 'before AttributeTransformManager: ' +
|
|
|
|
JSON.stringify( attributes, null, 2 ) );
|
|
|
|
new AttributeTransformManager(
|
|
|
|
this.manager,
|
|
|
|
this._returnAttributes.bind( this, tplExpandData )
|
|
|
|
).process( attributes );
|
|
|
|
|
|
|
|
// Unblock finish
|
|
|
|
if ( ! tplExpandData.attribsAsync ) {
|
|
|
|
// Attributes were transformed synchronously
|
|
|
|
this.manager.env.dp (
|
|
|
|
'sync attribs for ' + JSON.stringify( tplExpandData.target ),
|
|
|
|
tplExpandData.expandedArgs
|
|
|
|
);
|
|
|
|
// All attributes are fully expanded synchronously (no IO was needed)
|
|
|
|
return this._expandTemplate ( tplExpandData );
|
|
|
|
} else {
|
|
|
|
// Async attribute expansion is going on
|
|
|
|
this.manager.env.dp( 'async return for ' + JSON.stringify( token ));
|
|
|
|
tplExpandData.overallAsync = true;
|
|
|
|
return { async: true };
|
|
|
|
}
|
2012-01-06 14:30:55 +00:00
|
|
|
};
|
|
|
|
|
2012-01-19 23:43:39 +00:00
|
|
|
/**
|
|
|
|
* Create positional (number) keys for arguments without explicit keys
|
|
|
|
*/
|
2012-02-08 15:10:30 +00:00
|
|
|
TemplateHandler.prototype._nameArgs = function ( attribs ) {
|
2012-01-17 22:29:26 +00:00
|
|
|
var n = 1,
|
|
|
|
out = [];
|
2012-02-08 15:10:30 +00:00
|
|
|
for ( var i = 0, l = attribs.length; i < l; i++ ) {
|
2012-01-21 03:03:33 +00:00
|
|
|
// FIXME: Also check for whitespace-only named args!
|
2012-02-08 15:10:30 +00:00
|
|
|
if ( ! attribs[i].k.length ) {
|
|
|
|
out.push( {k: [ n.toString() ], v: attribs[i].v } );
|
2012-01-17 22:29:26 +00:00
|
|
|
n++;
|
|
|
|
} else {
|
2012-02-08 15:10:30 +00:00
|
|
|
out.push( attribs[i] );
|
2012-01-17 22:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
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-02-09 13:44:20 +00:00
|
|
|
/**
|
|
|
|
* Callback for argument (including target) expansion in AttributeTransformManager
|
|
|
|
*/
|
|
|
|
TemplateHandler.prototype._returnAttributes = function ( tplExpandData,
|
|
|
|
attributes )
|
|
|
|
{
|
|
|
|
this.manager.env.dp( 'TemplateHandler._returnAttributes: ' + JSON.stringify(attributes) );
|
|
|
|
// Remove the target from the attributes
|
|
|
|
tplExpandData.attribsAsync = false;
|
|
|
|
tplExpandData.target = attributes[0].v;
|
|
|
|
attributes.shift();
|
|
|
|
tplExpandData.expandedArgs = attributes;
|
|
|
|
if ( tplExpandData.overallAsync ) {
|
|
|
|
this._expandTemplate ( tplExpandData );
|
|
|
|
}
|
|
|
|
};
|
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-02-14 20:56:14 +00:00
|
|
|
//console.warn('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 ) {
|
2012-02-08 15:10:30 +00:00
|
|
|
tplExpandData.overallAsync = true;
|
2012-02-09 13:44:20 +00:00
|
|
|
this.manager.env.dp( 'Async return from _expandTemplate for ' +
|
|
|
|
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-02-13 14:17:03 +00:00
|
|
|
// Strip 'end' tokens and trailing newlines
|
|
|
|
var l = res[res.length - 1];
|
|
|
|
while ( res.length &&
|
|
|
|
( l.type === 'END' || l.constructor === NlTk )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
this.manager.env.dp( 'TemplateHandler, stripping end or whitespace tokens' );
|
2012-01-17 20:01:21 +00:00
|
|
|
res.pop();
|
2012-02-13 14:17:03 +00:00
|
|
|
l = res[res.length - 1];
|
2012-01-17 20:01:21 +00:00
|
|
|
}
|
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
// Could also encapsulate the template tokens here, if that turns out
|
|
|
|
// better for the editor.
|
|
|
|
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'TemplateHandler._onEnd: ' + JSON.stringify( res, null, 2 ) );
|
2012-01-13 18:48:25 +00:00
|
|
|
|
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 ) {
|
2012-02-09 13:44:20 +00:00
|
|
|
// Unwind the stack
|
|
|
|
process.nextTick(
|
|
|
|
function () {
|
|
|
|
callback( self.manager.env.pageCache[title], title );
|
|
|
|
}
|
|
|
|
);
|
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-02-09 13:44:20 +00:00
|
|
|
|
|
|
|
token.resultTokens = false;
|
|
|
|
|
|
|
|
new AttributeTransformManager (
|
|
|
|
this.manager,
|
|
|
|
this._returnArgAttributes.bind( this, token, cb, frame )
|
|
|
|
).process( token.attribs );
|
|
|
|
|
|
|
|
if ( token.resultTokens !== false ) {
|
|
|
|
// synchronous return
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'synchronous attribute expand: ' + JSON.stringify( token.resultTokens ) );
|
2012-02-08 15:10:30 +00:00
|
|
|
|
2012-02-09 13:44:20 +00:00
|
|
|
return { tokens: token.resultTokens };
|
|
|
|
} else {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'asynchronous attribute expand: ' + JSON.stringify( token, null, 2 ) );
|
2012-02-09 13:44:20 +00:00
|
|
|
// asynchronous return
|
|
|
|
token.resultTokens = [];
|
|
|
|
return { async: true };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TemplateHandler.prototype._returnArgAttributes = function ( token, cb, frame, attributes ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( '_returnArgAttributes: ' + JSON.stringify( attributes ));
|
2012-02-09 13:44:20 +00:00
|
|
|
var argName = this.manager.env.tokensToString( attributes[0].v ).trim(),
|
|
|
|
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-02-14 20:56:14 +00:00
|
|
|
//console.warn( '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-02-09 13:44:20 +00:00
|
|
|
var defaultValue = (attributes[1] && ! attributes[1].k.length && attributes[1].v) || false;
|
2012-01-21 20:38:13 +00:00
|
|
|
this.manager.env.dp( 'templateArg not found: ' + argName +
|
2012-02-08 15:10:30 +00:00
|
|
|
' vs. ' + JSON.stringify( defaultValue ) );
|
2012-02-09 13:44:20 +00:00
|
|
|
if ( defaultValue ) {
|
|
|
|
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-02-09 13:44:20 +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) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'response for ' + title + ' :' + body + ':' );
|
2012-01-20 02:36:18 +00:00
|
|
|
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 {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'body: ' + body );
|
2012-01-21 20:38:13 +00:00
|
|
|
data = JSON.parse( body );
|
2012-01-20 02:36:18 +00:00
|
|
|
} catch(e) {
|
2012-02-14 20:56:14 +00:00
|
|
|
console.warn( "Error: while parsing result. Error was: " );
|
|
|
|
console.warn( e );
|
|
|
|
console.warn( "Response that didn't parse was:");
|
|
|
|
console.warn( "------------------------------------------\n" + body );
|
|
|
|
console.warn( "------------------------------------------" );
|
2012-01-20 02:36:18 +00:00
|
|
|
}
|
|
|
|
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-02-14 20:56:14 +00:00
|
|
|
console.warn( 'Did not find page revisions in the returned body:' + body );
|
2012-01-20 02:36:18 +00:00
|
|
|
src = '';
|
|
|
|
}
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( '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) {
|
2012-02-14 20:56:14 +00:00
|
|
|
console.warn( 'Page ' + title + ' success ' + JSON.stringify( data ) );
|
2012-01-20 02:36:18 +00:00
|
|
|
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') {
|
2012-02-14 20:56:14 +00:00
|
|
|
console.warn( 'Page ' + title + 'not found! Got ' + src );
|
2012-01-20 02:36:18 +00:00
|
|
|
callback( 'Page ' + title + ' not found' );
|
|
|
|
} else {
|
|
|
|
// Add to cache
|
2012-02-14 20:56:14 +00:00
|
|
|
console.warn( 'Page ' + title + ': got ' + src );
|
2012-01-20 02:36:18 +00:00
|
|
|
this.manager.env.pageCache[title] = src;
|
|
|
|
callback(src, title);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function(xhr, msg, err) {
|
2012-02-14 20:56:14 +00:00
|
|
|
console.warn( 'Page/template fetch failure for title ' +
|
2012-01-20 02:36:18 +00:00
|
|
|
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;
|
|
|
|
}
|