From 21286a50dfe74cd51ba9307ba1af1073ba2294a9 Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Wed, 23 May 2012 16:43:45 +0200 Subject: [PATCH] Make sure pageName is set in the web service, and handle empty page name in parser function Change-Id: I5d36eefecc2f35a860d00a8960004f8e651ed17c --- api/ParserService.js | 7 ++++++- modules/parser/ext.core.ParserFunctions.js | 4 ++-- modules/parser/ext.core.TemplateHandler.js | 6 +++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/api/ParserService.js b/api/ParserService.js index 1c7bd73a68..41cdefc8e3 100644 --- a/api/ParserService.js +++ b/api/ParserService.js @@ -57,12 +57,14 @@ var textarea = function ( res, content ) { * Form-based HTML DOM -> wikitext interface for manual testing */ app.get(/\/_html\/(.*)/, function(req, res){ + env.pageName = req.params[0]; res.setHeader('Content-type', 'text/html; charset=UTF-8'); res.write( "Your HTML DOM:" ); textarea( res ); res.end(''); }); app.post(/\/_html\/(.*)/, function(req, res){ + env.pageName = req.params[0]; res.setHeader('Content-type', 'text/html; charset=UTF-8'); var p = new html5.Parser(); p.parse( req.body.content ); @@ -80,12 +82,14 @@ app.post(/\/_html\/(.*)/, function(req, res){ * Form-based wikitext -> HTML DOM interface for manual testing */ app.get(/\/_wikitext\/(.*)/, function(req, res){ + env.pageName = req.params[0]; res.setHeader('Content-type', 'text/html; charset=UTF-8'); res.write( "Your wikitext:" ); textarea( res ); res.end(''); }); app.post(/\/_wikitext\/(.*)/, function(req, res){ + env.pageName = req.params[0]; res.setHeader('Content-type', 'text/html; charset=UTF-8'); var parser = parserPipelineFactory.makePipeline( 'text/x-mediawiki/full' ); parser.on('document', function ( document ) { @@ -111,7 +115,7 @@ app.post(/\/_wikitext\/(.*)/, function(req, res){ * Regular article parsing */ app.get(/\/(.*)/, function(req, res){ - env.pageName = req.params.title; + env.pageName = req.params[0]; var parser = parserPipelineFactory.makePipeline( 'text/x-mediawiki/full' ); parser.on('document', function ( document ) { res.end(document.body.innerHTML); @@ -134,6 +138,7 @@ app.get(/\/(.*)/, function(req, res){ * Regular article serialization using POST */ app.post(/\/(.*)/, function(req, res){ + env.pageName = req.params[0]; res.setHeader('Content-type', 'text/plain; charset=UTF-8'); var p = new html5.Parser(); p.parse( req.body.content ); diff --git a/modules/parser/ext.core.ParserFunctions.js b/modules/parser/ext.core.ParserFunctions.js index 936ce4cb2e..533f6bb93a 100644 --- a/modules/parser/ext.core.ParserFunctions.js +++ b/modules/parser/ext.core.ParserFunctions.js @@ -644,10 +644,10 @@ ParserFunctions.prototype.pf_namespacee = function ( token, frame, cb, args ) { cb( { tokens: [target.split(':').pop() || 'Main'] } ); }; ParserFunctions.prototype.pf_pagename = function ( token, frame, cb, args ) { - cb( { tokens: [this.env.pageName] } ); + cb( { tokens: [this.env.pageName || ''] } ); }; ParserFunctions.prototype.pf_pagenamebase = function ( token, frame, cb, args ) { - cb( { tokens: [this.env.pageName] } ); + cb( { tokens: [this.env.pageName || ''] } ); }; ParserFunctions.prototype.pf_scriptpath = function ( token, frame, cb, args ) { cb( { tokens: [this.env.wgScriptPath] } ); diff --git a/modules/parser/ext.core.TemplateHandler.js b/modules/parser/ext.core.TemplateHandler.js index 19c9079be5..092f284d54 100644 --- a/modules/parser/ext.core.TemplateHandler.js +++ b/modules/parser/ext.core.TemplateHandler.js @@ -185,7 +185,7 @@ TemplateHandler.prototype._processTemplateAndTitle = function( token, frame, cb, // Hook up the inputPipeline output events to our handlers pipeline.addListener( 'chunk', this._onChunk.bind ( this, cb ) ); - pipeline.addListener( 'end', this._onEnd.bind ( this ) ); + pipeline.addListener( 'end', this._onEnd.bind ( this, cb ) ); // Feed the pipeline. XXX: Support different formats. this.manager.env.dp( 'TemplateHandler._processTemplateAndTitle', name, attribs ); pipeline.process ( src, name ); @@ -205,9 +205,9 @@ TemplateHandler.prototype._onChunk = function( cb, chunk ) { * Handle the end event emitted by the parser pipeline after fully processing * the template source. */ -TemplateHandler.prototype._onEnd = function( token, frame, cb ) { +TemplateHandler.prototype._onEnd = function( cb ) { this.manager.env.dp( 'TemplateHandler._onEnd' ); - cb( { tokens: [token] } ); + cb( { tokens: [] } ); };