r109426 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109425‎ | r109426 | r109427 >
Date:19:38, 18 January 2012
Author:gwicke
Status:deferred
Tags:
Comment:
Make template fetching somewhat functional on node with Inez' help, but
disable it by default in parserTests as it tries to fetch all sorts of parser
functions and is not yet fully supported in parserTests. The next step will be
to build a list of parser functions (to avoid fetching them as templates) and
pushing the event interface into parserTests.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js
@@ -10,7 +10,10 @@
1111 * @author Brion Vibber <brion@wikimedia.org>
1212 */
1313 var $ = require('jquery'),
14 - AttributeTransformManager = require('./mediawiki.TokenTransformManager.js').AttributeTransformManager;
 14+ request = require('request'),
 15+ qs = require('querystring'),
 16+ AttributeTransformManager = require('./mediawiki.TokenTransformManager.js')
 17+ .AttributeTransformManager;
1518
1619
1720 function TemplateHandler ( manager ) {
@@ -78,7 +81,8 @@
7982 var attributes = [[[{ type: 'TEXT', value: '' }] , token.target ]]
8083 .concat( this._nameArgs( token.orderedArgs ) );
8184
82 - //console.log( 'before AttributeTransformManager: ' + JSON.stringify( attributes, null, 2 ) );
 85+ //console.log( 'before AttributeTransformManager: ' +
 86+ // JSON.stringify( attributes, null, 2 ) );
8387 new AttributeTransformManager(
8488 this.manager,
8589 this._returnAttributes.bind( this, templateTokenTransformData )
@@ -88,6 +92,7 @@
8993 templateTokenTransformData.outstanding--;
9094 if ( templateTokenTransformData.outstanding === 0 ) {
9195 //console.log( 'direct call');
 96+ // All attributes are fully expanded synchronously (no IO was needed)
9297 return this._expandTemplate ( templateTokenTransformData );
9398 } else {
9499 templateTokenTransformData.isAsync = true;
@@ -110,7 +115,9 @@
111116 return out;
112117 };
113118
114 -TemplateHandler.prototype._returnAttributes = function ( templateTokenTransformData, attributes ) {
 119+TemplateHandler.prototype._returnAttributes = function ( templateTokenTransformData,
 120+ attributes )
 121+{
115122 //console.log( 'TemplateHandler._returnAttributes: ' + JSON.stringify(attributes) );
116123 // Remove the target from the attributes
117124 templateTokenTransformData.target = attributes[0][1];
@@ -182,7 +189,9 @@
183190 target,
184191 'Template'
185192 );
186 - this._fetchTemplateAndTitle( templateName, this._processTemplateAndTitle.bind( this, inputPipeline ) );
 193+ this._fetchTemplateAndTitle( templateName,
 194+ this._processTemplateAndTitle.bind( this, inputPipeline )
 195+ );
187196
188197 // Set up a pipeline:
189198 // fetch template source -> tokenizer
@@ -204,7 +213,6 @@
205214 // fetch from DB or interwiki
206215 // infinte loop check
207216
208 - // Always asynchronous..
209217 if ( this.isAsync ) {
210218 return {};
211219 } else {
@@ -275,23 +283,81 @@
276284 */
277285 TemplateHandler.prototype._fetchTemplateAndTitle = function( title, callback ) {
278286 // @fixme normalize name?
 287+ var self = this;
279288 if (title in this.manager.env.pageCache) {
280289 // @fixme should this be forced to run on next event?
281290 callback( this.manager.env.pageCache[title], title );
 291+ } else if ( ! this.manager.env.fetchTemplates ) {
 292+ callback('Page/template fetching disabled, and no cache for ' + title);
282293 } else {
283294 // whee fun hack!
284 - //console.log(title);
 295+
 296+ this.isAsync = true;
 297+ console.log( 'trying to fetch ' + title );
285298 //console.log(this.manager.env.pageCache);
 299+ var url = this.manager.env.wgScriptPath + '/api' +
 300+ this.manager.env.wgScriptExtension +
 301+ '?format=json&action=query&prop=revisions&rvprop=content&titles=' + title;
 302+
 303+ request({
 304+ method: 'GET',
 305+ //followRedirect: false,
 306+ url: url,
 307+ headers: {
 308+ 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1 Iceweasel/9.0.1'
 309+ }
 310+ },
 311+ function (error, response, body) {
 312+ console.log( 'response for ' + title + ': ' + body );
 313+ if(error) {
 314+ console.log(error);
 315+ callback('Page/template fetch failure for title ' + title);
 316+ return ;
 317+ }
 318+
 319+ if(response.statusCode == 200) {
 320+ try{
 321+ console.log( 'body: ' + body );
 322+ data = JSON.parse(body);
 323+ var src = null;
 324+ $.each(data.query.pages, function(i, page) {
 325+ if (page.revisions && page.revisions.length) {
 326+ src = page.revisions[0]['*'];
 327+ title = page.title;
 328+ }
 329+ });
 330+ console.log( 'Page ' + title + ': got ' + src );
 331+ self.manager.env.pageCache[title] = src;
 332+ callback(src, title);
 333+ } catch(e) {
 334+ console.log("Error: while parsing result. Error was: ");
 335+ console.log(e);
 336+ console.log("Response that didn't parse was:\n" + body);
 337+
 338+ data = {
 339+ error: '',
 340+ errorWfMsg: 'chat-err-communicating-with-mediawiki',
 341+ errorMsgParams: []
 342+ };
 343+ }
 344+ console.log(data);
 345+ }
 346+ });
 347+
 348+ /*
 349+ * XXX: The jQuery version does not quite work with node, but we keep
 350+ * it around for now.
286351 $.ajax({
287 - url: this.manager.env.wgScriptPath + '/api' + this.manager.env.wgScriptExtension,
 352+ url: url,
288353 data: {
289354 format: 'json',
290 - action: 'query',
291 - prop: 'revisions',
292 - rvprop: 'content',
293 - titles: title
 355+ action: 'query',
 356+ prop: 'revisions',
 357+ rvprop: 'content',
 358+ titles: title
294359 },
295 - success: function(data, xhr) {
 360+ success: function(data, statusString, xhr) {
 361+ console.log( 'Page ' + title + ' success ' + JSON.stringify( data ) );
296362 var src = null, title = null;
297363 $.each(data.query.pages, function(i, page) {
298364 if (page.revisions && page.revisions.length) {
@@ -300,19 +366,25 @@
301367 }
302368 });
303369 if (typeof src !== 'string') {
304 - //console.log( 'Page ' + title + 'not found!' );
 370+ console.log( 'Page ' + title + 'not found! Got ' + src );
305371 callback( 'Page ' + title + ' not found' );
306372 } else {
 373+ // Add to cache
 374+ console.log( 'Page ' + title + ': got ' + src );
 375+ this.manager.env.pageCache[title] = src;
307376 callback(src, title);
308377 }
309378 },
310 - error: function(msg) {
311 - //console.log( 'Page/template fetch failure for title ' + title );
 379+ error: function(xhr, msg, err) {
 380+ console.log( 'Page/template fetch failure for title ' +
 381+ title + ', url=' + url + JSON.stringify(xhr) + ', err=' + err );
312382 callback('Page/template fetch failure for title ' + title);
313383 },
314384 dataType: 'json',
315 - cache: false // @fixme caching, versions etc?
316 - }, 'json');
 385+ cache: false, // @fixme caching, versions etc?
 386+ crossDomain: true
 387+ });
 388+ */
317389 }
318390 };
319391
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
@@ -3,14 +3,13 @@
44 tagHooks: {},
55 parserFunctions: {},
66 pageCache: {}, // @fixme use something with managed space
7 - domCache: {}
 7+ debug: false,
 8+ wgScriptPath: "http://en.wikipedia.org/w",
 9+ wgScriptExtension: ".php",
 10+ fetchTemplates: false
811 };
912 $.extend(options, opts);
10 - this.debug = false;
11 - this.tagHooks = options.tagHooks;
12 - this.parserFunctions = options.parserFunctions;
13 - this.pageCache = options.pageCache;
14 - this.domCache = options.domCache;
 13+ $.extend(this, options);
1514 };
1615
1716 MWParserEnvironment.prototype.lookupKV = function ( kvs, key ) {

Status & tagging log