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 ) {
|
|
|
|
this.register( manager );
|
2012-01-21 20:38:13 +00:00
|
|
|
this.parserFunctions = new ParserFunctions( manager );
|
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-04-25 14:35:59 +00:00
|
|
|
// expand argument keys, with callback set to next processing step
|
|
|
|
// XXX: would likely be faster to do this in a tight loop here
|
|
|
|
var atm = new AttributeTransformManager(
|
2012-02-09 13:44:20 +00:00
|
|
|
this.manager,
|
2012-04-25 14:35:59 +00:00
|
|
|
this._expandTemplate.bind( this, token, frame, cb )
|
|
|
|
);
|
|
|
|
cb( { async: true } );
|
|
|
|
atm.processKeys( token.attribs );
|
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 ) {
|
2012-05-10 08:04:24 +00:00
|
|
|
out.push( new KV( n.toString(), 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-02-21 18:49:43 +00:00
|
|
|
this.manager.env.dp( '_nameArgs: ', out );
|
2012-01-17 22:29:26 +00:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
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-04-25 14:35:59 +00:00
|
|
|
TemplateHandler.prototype._expandTemplate = function ( token, frame, cb, attribs ) {
|
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-04-25 14:35:59 +00:00
|
|
|
var target = attribs[0].k;
|
2012-01-20 00:49:27 +00:00
|
|
|
|
2012-01-19 23:43:39 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( ! target ) {
|
|
|
|
this.manager.env.ap( 'No target! ', attribs );
|
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-04-25 14:35:59 +00:00
|
|
|
target = this.manager.env.tokensToString( target ).trim();
|
2012-01-22 07:07:16 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
//var args = this.manager.env.KVtoHash( tplExpandData.expandedArgs );
|
2012-01-21 20:38:13 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
// strip subst for now.
|
2012-04-17 09:02:52 +00:00
|
|
|
target = target.replace( /^(safe)?subst:/, '' );
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
// XXX: wrap attribs in object with .dict() and .named() methods,
|
|
|
|
// and each member (key/value) into object with .tokens(), .dom() and
|
|
|
|
// .wikitext() methods (subclass of Array)
|
|
|
|
|
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 ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
var pfAttribs = new Params( this.manager.env, attribs );
|
|
|
|
pfAttribs[0] = new KV( target.substr( prefix.length + 1 ), [] );
|
|
|
|
//this.manager.env.dp( 'func prefix/args: ', prefix,
|
|
|
|
// tplExpandData.expandedArgs,
|
|
|
|
// 'unnamedArgs', tplExpandData.origToken.attribs,
|
|
|
|
// 'funcArg:', funcArg
|
|
|
|
// );
|
2012-05-10 08:04:24 +00:00
|
|
|
this.manager.env.dp( 'entering prefix', target, token );
|
2012-04-25 14:35:59 +00:00
|
|
|
this.parserFunctions[ 'pf_' + prefix ]
|
|
|
|
( token, this.manager.frame, cb, pfAttribs );
|
|
|
|
return;
|
2012-01-21 20:38:13 +00:00
|
|
|
}
|
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-04-25 14:35:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Resolve a possibly relative link
|
|
|
|
var templateName = this.manager.env.resolveTitle(
|
|
|
|
target,
|
|
|
|
'Template'
|
|
|
|
);
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
var checkRes = this.manager.frame.loopAndDepthCheck( templateName, 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}] ),
|
2012-04-25 14:35:59 +00:00
|
|
|
templateName,
|
2012-02-01 16:30:43 +00:00
|
|
|
new EndTagTk( 'a' )
|
2012-01-22 01:27:22 +00:00
|
|
|
];
|
2012-05-10 08:04:24 +00:00
|
|
|
res.rank = this.manager.phaseEndRank;
|
|
|
|
cb( { tokens: res } );
|
2012-04-25 14:35:59 +00:00
|
|
|
return;
|
2012-01-14 00:58:20 +00:00
|
|
|
}
|
2012-01-10 01:09:50 +00:00
|
|
|
|
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,
|
2012-04-25 14:35:59 +00:00
|
|
|
cb,
|
|
|
|
this._processTemplateAndTitle.bind( this, token, frame, cb, templateName, attribs )
|
2012-01-19 23:43:39 +00:00
|
|
|
);
|
2012-01-06 14:30:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
/**
|
|
|
|
* Process a fetched template source
|
|
|
|
*/
|
|
|
|
TemplateHandler.prototype._processTemplateAndTitle = function( token, frame, cb, name, attribs, src, type ) {
|
|
|
|
// 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).
|
|
|
|
var pipeline = this.manager.pipeFactory.getPipeline(
|
2012-04-25 18:19:43 +00:00
|
|
|
type || 'text/x-mediawiki', true
|
2012-04-25 14:35:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
pipeline.setFrame( this.manager.frame, name, attribs );
|
|
|
|
|
|
|
|
// Hook up the inputPipeline output events to our handlers
|
|
|
|
pipeline.addListener( 'chunk', this._onChunk.bind ( this, cb ) );
|
2012-05-23 14:43:45 +00:00
|
|
|
pipeline.addListener( 'end', this._onEnd.bind ( this, cb ) );
|
2012-04-25 14:35:59 +00:00
|
|
|
// Feed the pipeline. XXX: Support different formats.
|
2012-05-10 08:04:24 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler._processTemplateAndTitle', name, attribs );
|
2012-04-25 14:35:59 +00:00
|
|
|
pipeline.process ( src, name );
|
|
|
|
};
|
|
|
|
|
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-04-25 14:35:59 +00:00
|
|
|
TemplateHandler.prototype._onChunk = function( cb, chunk ) {
|
2012-01-10 01:09:50 +00:00
|
|
|
// We encapsulate the output by default, so collect tokens here.
|
2012-05-10 08:04:24 +00:00
|
|
|
chunk = this.manager.env.stripEOFTkfromTokens( chunk );
|
2012-02-21 18:49:43 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler._onChunk', chunk );
|
2012-04-25 14:35:59 +00:00
|
|
|
cb( { tokens: chunk, async: true } );
|
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-05-23 14:43:45 +00:00
|
|
|
TemplateHandler.prototype._onEnd = function( cb ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
this.manager.env.dp( 'TemplateHandler._onEnd' );
|
2012-05-23 14:43:45 +00:00
|
|
|
cb( { tokens: [] } );
|
2012-04-16 13:47:03 +00:00
|
|
|
};
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2012-01-06 17:19:14 +00:00
|
|
|
/**
|
|
|
|
* Fetch a template
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
TemplateHandler.prototype._fetchTemplateAndTitle = function ( title, parentCB, cb ) {
|
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-04-25 18:19:43 +00:00
|
|
|
// XXX: store type too (and cache tokens/x-mediawiki)
|
2012-04-25 14:35:59 +00:00
|
|
|
cb( self.manager.env.pageCache[title] /* , type */ );
|
2012-01-18 19:38:32 +00:00
|
|
|
} else if ( ! this.manager.env.fetchTemplates ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
parentCB( { tokens: [ 'Warning: Page/template fetching disabled, and no cache for ' +
|
|
|
|
title ] } );
|
2012-01-06 17:19:14 +00:00
|
|
|
} else {
|
2012-01-20 02:36:18 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
// We are about to start an async request for a template
|
2012-02-21 18:49:43 +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-05-10 08:04:24 +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 );
|
|
|
|
}
|
2012-04-16 13:47:03 +00:00
|
|
|
// Append a listener to the request at the toplevel, but prepend at
|
|
|
|
// lower levels to enforce depth-first processing
|
2012-04-18 15:53:04 +00:00
|
|
|
if ( false && this.manager.isInclude ) {
|
2012-04-16 13:47:03 +00:00
|
|
|
// prepend request: deal with requests from includes first
|
|
|
|
this.manager.env.requestQueue[title]
|
2012-04-25 14:35:59 +00:00
|
|
|
.listeners( 'src' ).unshift( cb );
|
2012-04-16 13:47:03 +00:00
|
|
|
} else {
|
|
|
|
// append request, process in document order
|
|
|
|
this.manager.env.requestQueue[title]
|
2012-04-25 14:35:59 +00:00
|
|
|
.listeners( 'src' ).push( cb );
|
2012-04-16 13:47:03 +00:00
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
parentCB ( { async: true } );
|
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
|
|
|
new AttributeTransformManager (
|
|
|
|
this.manager,
|
|
|
|
this._returnArgAttributes.bind( this, token, cb, frame )
|
2012-04-18 12:30:59 +00:00
|
|
|
).process( token.attribs.slice() );
|
2012-02-09 13:44:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TemplateHandler.prototype._returnArgAttributes = function ( token, cb, frame, attributes ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( '_returnArgAttributes: ' + JSON.stringify( attributes ));
|
2012-05-10 08:04:24 +00:00
|
|
|
var argName = this.manager.env.tokensToString( attributes[0].k ).trim(),
|
2012-04-25 14:35:59 +00:00
|
|
|
res,
|
|
|
|
dict = this.manager.frame.args.named();
|
2012-05-10 08:04:24 +00:00
|
|
|
this.manager.env.dp( 'args', argName /*, dict*/ );
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( argName in dict ) {
|
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 ) );
|
2012-04-25 14:35:59 +00:00
|
|
|
res = dict[argName];
|
2012-05-10 08:04:24 +00:00
|
|
|
this.manager.env.dp( 'arg res:', res );
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( res.constructor === String ) {
|
|
|
|
cb( { tokens: [res] } );
|
|
|
|
} else {
|
2012-05-10 08:04:24 +00:00
|
|
|
dict[argName].get({
|
|
|
|
type: 'tokens/x-mediawiki/expanded',
|
|
|
|
cb: function( res ) { cb ( { tokens: res } ); },
|
|
|
|
asyncCB: cb
|
|
|
|
});
|
2012-04-25 14:35:59 +00:00
|
|
|
}
|
|
|
|
return;
|
2012-01-06 14:30:55 +00:00
|
|
|
} else {
|
2012-05-10 08:04:24 +00:00
|
|
|
this.manager.env.dp( 'templateArg not found: ', argName
|
|
|
|
/*' vs. ', dict */ );
|
2012-03-14 16:30:59 +00:00
|
|
|
if ( attributes.length > 1 ) {
|
|
|
|
res = attributes[1].v;
|
2012-01-06 17:19:14 +00:00
|
|
|
} else {
|
2012-03-14 16:30:59 +00:00
|
|
|
//console.warn('no default for ' + argName + JSON.stringify( attributes ));
|
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-04-25 14:35:59 +00:00
|
|
|
cb( { tokens: 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-04-25 14:35:59 +00:00
|
|
|
this.setMaxListeners( 50000 );
|
2012-04-26 17:51:00 +00:00
|
|
|
this.retries = 5;
|
|
|
|
this.manager = manager;
|
|
|
|
this.title = title;
|
2012-04-26 18:31:23 +00:00
|
|
|
var url = manager.env.wgScript + '/api' +
|
2012-01-20 02:36:18 +00:00
|
|
|
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;
|
|
|
|
|
2012-04-26 18:31:23 +00:00
|
|
|
this.requestOptions = {
|
2012-01-20 02:36:18 +00:00
|
|
|
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'
|
2012-04-26 18:31:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Start the request
|
|
|
|
request( this.requestOptions, this._handler.bind(this) );
|
2012-04-26 17:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inherit from EventEmitter
|
|
|
|
TemplateRequest.prototype = new events.EventEmitter();
|
2012-05-10 08:04:24 +00:00
|
|
|
TemplateRequest.prototype.constructor = TemplateRequest;
|
2012-04-26 17:51:00 +00:00
|
|
|
|
|
|
|
TemplateRequest.prototype._handler = function (error, response, body) {
|
|
|
|
//console.warn( 'response for ' + title + ' :' + body + ':' );
|
|
|
|
if(error) {
|
|
|
|
this.manager.env.dp(error);
|
|
|
|
if ( this.retries ) {
|
|
|
|
this.retries--;
|
|
|
|
this.manager.env.tp( 'Retrying template request for ' + this.title );
|
|
|
|
var self = this;
|
|
|
|
// retry
|
2012-04-26 18:31:23 +00:00
|
|
|
request( this.requestOptions, this._handler.bind(this) );
|
2012-04-26 17:51:00 +00:00
|
|
|
} else {
|
|
|
|
this.emit('src', 'Page/template fetch failure for title ' + this.title,
|
|
|
|
'text/x-mediawiki');
|
|
|
|
}
|
|
|
|
} else if(response.statusCode == 200) {
|
|
|
|
var src = '',
|
|
|
|
data,
|
2012-05-03 13:54:36 +00:00
|
|
|
normalizedTitle;
|
2012-04-26 17:51:00 +00:00
|
|
|
try {
|
|
|
|
//console.warn( 'body: ' + body );
|
|
|
|
data = JSON.parse( body );
|
|
|
|
} catch(e) {
|
|
|
|
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( "------------------------------------------" );
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$.each( data.query.pages, function(i, page) {
|
|
|
|
if (page.revisions && page.revisions.length) {
|
|
|
|
src = page.revisions[0]['*'];
|
|
|
|
normalizeTitle = page.title;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch ( e2 ) {
|
|
|
|
console.warn( 'Did not find page revisions in the returned body:' + body );
|
|
|
|
src = '';
|
|
|
|
}
|
2012-05-03 13:54:36 +00:00
|
|
|
|
|
|
|
// check for #REDIRECT
|
2012-05-10 08:04:24 +00:00
|
|
|
var redirMatch = src.match( /[\r\n\s]*#\s*redirect\s\[\[([^\]]+)\]\]/i )
|
2012-05-03 13:54:36 +00:00
|
|
|
if ( redirMatch ) {
|
|
|
|
var title = redirMatch[1];
|
|
|
|
var url = this.manager.env.wgScript + '/api' +
|
|
|
|
this.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;
|
|
|
|
this.requestOptions.url = url;
|
|
|
|
request( this.requestOptions, this._handler.bind(this) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-26 17:51:00 +00:00
|
|
|
//console.warn( 'Page ' + title + ': got ' + src );
|
2012-05-10 08:04:24 +00:00
|
|
|
this.manager.env.tp( 'Retrieved ' + this.title, src );
|
2012-04-26 17:51:00 +00:00
|
|
|
|
|
|
|
// Add the source to the cache
|
|
|
|
this.manager.env.pageCache[this.title] = src;
|
|
|
|
|
|
|
|
// Process only a few callbacks in each event loop iteration to
|
|
|
|
// reduce memory usage.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
var listeners = this.listeners( 'src' );
|
|
|
|
var processSome = function () {
|
2012-05-10 08:04:24 +00:00
|
|
|
// XXX: experiment a bit with the number of callbacks per
|
2012-04-26 17:51:00 +00:00
|
|
|
// iteration!
|
|
|
|
var maxIters = Math.min(1, listeners.length);
|
|
|
|
for ( var it = 0; it < maxIters; it++ ) {
|
|
|
|
var nextListener = listeners.shift();
|
|
|
|
// We only retrieve text/x-mediawiki source currently.
|
|
|
|
nextListener( src, 'text/x-mediawiki' );
|
2012-01-20 02:36:18 +00:00
|
|
|
}
|
2012-04-26 17:51:00 +00:00
|
|
|
if ( listeners.length ) {
|
|
|
|
process.nextTick( processSome );
|
2012-01-20 02:36:18 +00:00
|
|
|
}
|
2012-04-26 17:51:00 +00:00
|
|
|
};
|
2012-04-13 12:31:03 +00:00
|
|
|
|
2012-04-26 17:51:00 +00:00
|
|
|
process.nextTick( processSome );
|
|
|
|
//processSome();
|
2012-04-13 12:31:03 +00:00
|
|
|
|
2012-04-26 17:51:00 +00:00
|
|
|
//self.emit( 'src', src, title );
|
|
|
|
}
|
|
|
|
// XXX: handle other status codes
|
2012-01-20 18:47:17 +00:00
|
|
|
|
2012-04-26 17:51:00 +00:00
|
|
|
// Remove self from request queue
|
2012-05-10 08:04:24 +00:00
|
|
|
//this.manager.env.dp( 'trying to remove ', this.title, ' from requestQueue' );
|
2012-04-26 17:51:00 +00:00
|
|
|
delete this.manager.env.requestQueue[this.title];
|
2012-05-10 08:04:24 +00:00
|
|
|
//this.manager.env.dp( 'after deletion:', this.manager.env.requestQueue );
|
2012-04-26 17:51:00 +00:00
|
|
|
};
|
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
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-06 14:30:55 +00:00
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.TemplateHandler = TemplateHandler;
|
|
|
|
}
|