Index: trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js |
— | — | @@ -10,7 +10,10 @@ |
11 | 11 | * @author Brion Vibber <brion@wikimedia.org> |
12 | 12 | */ |
13 | 13 | 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; |
15 | 18 | |
16 | 19 | |
17 | 20 | function TemplateHandler ( manager ) { |
— | — | @@ -78,7 +81,8 @@ |
79 | 82 | var attributes = [[[{ type: 'TEXT', value: '' }] , token.target ]] |
80 | 83 | .concat( this._nameArgs( token.orderedArgs ) ); |
81 | 84 | |
82 | | - //console.log( 'before AttributeTransformManager: ' + JSON.stringify( attributes, null, 2 ) ); |
| 85 | + //console.log( 'before AttributeTransformManager: ' + |
| 86 | + // JSON.stringify( attributes, null, 2 ) ); |
83 | 87 | new AttributeTransformManager( |
84 | 88 | this.manager, |
85 | 89 | this._returnAttributes.bind( this, templateTokenTransformData ) |
— | — | @@ -88,6 +92,7 @@ |
89 | 93 | templateTokenTransformData.outstanding--; |
90 | 94 | if ( templateTokenTransformData.outstanding === 0 ) { |
91 | 95 | //console.log( 'direct call'); |
| 96 | + // All attributes are fully expanded synchronously (no IO was needed) |
92 | 97 | return this._expandTemplate ( templateTokenTransformData ); |
93 | 98 | } else { |
94 | 99 | templateTokenTransformData.isAsync = true; |
— | — | @@ -110,7 +115,9 @@ |
111 | 116 | return out; |
112 | 117 | }; |
113 | 118 | |
114 | | -TemplateHandler.prototype._returnAttributes = function ( templateTokenTransformData, attributes ) { |
| 119 | +TemplateHandler.prototype._returnAttributes = function ( templateTokenTransformData, |
| 120 | + attributes ) |
| 121 | +{ |
115 | 122 | //console.log( 'TemplateHandler._returnAttributes: ' + JSON.stringify(attributes) ); |
116 | 123 | // Remove the target from the attributes |
117 | 124 | templateTokenTransformData.target = attributes[0][1]; |
— | — | @@ -182,7 +189,9 @@ |
183 | 190 | target, |
184 | 191 | 'Template' |
185 | 192 | ); |
186 | | - this._fetchTemplateAndTitle( templateName, this._processTemplateAndTitle.bind( this, inputPipeline ) ); |
| 193 | + this._fetchTemplateAndTitle( templateName, |
| 194 | + this._processTemplateAndTitle.bind( this, inputPipeline ) |
| 195 | + ); |
187 | 196 | |
188 | 197 | // Set up a pipeline: |
189 | 198 | // fetch template source -> tokenizer |
— | — | @@ -204,7 +213,6 @@ |
205 | 214 | // fetch from DB or interwiki |
206 | 215 | // infinte loop check |
207 | 216 | |
208 | | - // Always asynchronous.. |
209 | 217 | if ( this.isAsync ) { |
210 | 218 | return {}; |
211 | 219 | } else { |
— | — | @@ -275,23 +283,81 @@ |
276 | 284 | */ |
277 | 285 | TemplateHandler.prototype._fetchTemplateAndTitle = function( title, callback ) { |
278 | 286 | // @fixme normalize name? |
| 287 | + var self = this; |
279 | 288 | if (title in this.manager.env.pageCache) { |
280 | 289 | // @fixme should this be forced to run on next event? |
281 | 290 | 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); |
282 | 293 | } else { |
283 | 294 | // whee fun hack! |
284 | | - //console.log(title); |
| 295 | + |
| 296 | + this.isAsync = true; |
| 297 | + console.log( 'trying to fetch ' + title ); |
285 | 298 | //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. |
286 | 351 | $.ajax({ |
287 | | - url: this.manager.env.wgScriptPath + '/api' + this.manager.env.wgScriptExtension, |
| 352 | + url: url, |
288 | 353 | data: { |
289 | 354 | 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 |
294 | 359 | }, |
295 | | - success: function(data, xhr) { |
| 360 | + success: function(data, statusString, xhr) { |
| 361 | + console.log( 'Page ' + title + ' success ' + JSON.stringify( data ) ); |
296 | 362 | var src = null, title = null; |
297 | 363 | $.each(data.query.pages, function(i, page) { |
298 | 364 | if (page.revisions && page.revisions.length) { |
— | — | @@ -300,19 +366,25 @@ |
301 | 367 | } |
302 | 368 | }); |
303 | 369 | if (typeof src !== 'string') { |
304 | | - //console.log( 'Page ' + title + 'not found!' ); |
| 370 | + console.log( 'Page ' + title + 'not found! Got ' + src ); |
305 | 371 | callback( 'Page ' + title + ' not found' ); |
306 | 372 | } else { |
| 373 | + // Add to cache |
| 374 | + console.log( 'Page ' + title + ': got ' + src ); |
| 375 | + this.manager.env.pageCache[title] = src; |
307 | 376 | callback(src, title); |
308 | 377 | } |
309 | 378 | }, |
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 ); |
312 | 382 | callback('Page/template fetch failure for title ' + title); |
313 | 383 | }, |
314 | 384 | dataType: 'json', |
315 | | - cache: false // @fixme caching, versions etc? |
316 | | - }, 'json'); |
| 385 | + cache: false, // @fixme caching, versions etc? |
| 386 | + crossDomain: true |
| 387 | + }); |
| 388 | + */ |
317 | 389 | } |
318 | 390 | }; |
319 | 391 | |
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js |
— | — | @@ -3,14 +3,13 @@ |
4 | 4 | tagHooks: {}, |
5 | 5 | parserFunctions: {}, |
6 | 6 | 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 |
8 | 11 | }; |
9 | 12 | $.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); |
15 | 14 | }; |
16 | 15 | |
17 | 16 | MWParserEnvironment.prototype.lookupKV = function ( kvs, key ) { |