r113162 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113161‎ | r113162 | r113163 >
Date:18:02, 6 March 2012
Author:gwicke
Status:deferred
Tags:
Comment:
Improve generic attribute expansion before external link processing, and make
wgUploadPath configurable. Also change the hard-coded fall-back image sizes to
sensible defaults. This breaks three parser tests until image size retrieval
from the wiki is implemented.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.AttributeExpander.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.Title.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/parse.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/parser/parserTests.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/parser/parserTests.js
@@ -202,7 +202,7 @@
203203 fetchTemplates: false,
204204 debug: this.argv.debug,
205205 trace: this.argv.trace,
206 - wgScriptPath: '/wiki/'
 206+ wgUploadPath: 'http://example.com/images'
207207 });
208208 }
209209
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js
@@ -91,6 +91,7 @@
9292 transArr.push(transformer);
9393 // sort ascending by rank
9494 transArr.sort( this._cmpTransformations );
 95+ this.env.dp( 'transforms: ', this.transformers );
9596 };
9697
9798 /**
@@ -191,7 +192,7 @@
192193 * processed tokens.
193194 * @returns {Object} Token(s) and async indication.
194195 */
195 -TokenTransformManager.prototype._transformTagToken = function ( token, phaseEndRank, cbOrPrevToken ) {
 196+TokenTransformManager.prototype._transformTagToken = function ( token, cbOrPrevToken ) {
196197 // prepend 'any' transformers
197198 var ts = this.transformers.any,
198199 res = { token: token },
@@ -205,6 +206,7 @@
206207 // could cache this per tag type to avoid re-sorting each time
207208 ts = ts.concat(tagts);
208209 ts.sort( this._cmpTransformations );
 210+ this.env.dp( 'ts: ', ts );
209211 }
210212 //console.warn(JSON.stringify(ts, null, 2));
211213 if ( ts ) {
@@ -243,7 +245,7 @@
244246 if ( res.token.rank === undefined && res.token.constructor === String ) {
245247 res.token = new String ( res.token );
246248 }
247 - res.token.rank = phaseEndRank;
 249+ res.token.rank = this.phaseEndRank;
248250 }
249251 }
250252 return res;
@@ -261,7 +263,7 @@
262264 * @param {Array} ts List of token transformers for this token type.
263265 * @returns {Object} Token(s) and async indication.
264266 */
265 -TokenTransformManager.prototype._transformToken = function ( token, phaseEndRank, ts, cbOrPrevToken ) {
 267+TokenTransformManager.prototype._transformToken = function ( token, ts, cbOrPrevToken ) {
266268 // prepend 'any' transformers
267269 //this.env.dp('_transformToken', token);
268270 var anyTrans = this.transformers.any;
@@ -303,7 +305,7 @@
304306 if ( res.token.rank === undefined && res.token.constructor === String ) {
305307 res.token = new String ( res.token );
306308 }
307 - res.token.rank = phaseEndRank; // need phase passed in!
 309+ res.token.rank = this.phaseEndRank; // need phase passed in!
308310 }
309311 //else {
310312 // this.env.dp( '_transformToken aborted', res );
@@ -333,7 +335,7 @@
334336 * @param {Object} args, the argument map for templates
335337 * @param {Object} env, the environment.
336338 */
337 -function AsyncTokenTransformManager ( childFactories, args, env, inputType ) {
 339+function AsyncTokenTransformManager ( childFactories, args, env, inputType, phaseEndRank ) {
338340 // Factory function for new AsyncTokenTransformManager creation with
339341 // default transforms enabled
340342 // Also sets up a tokenizer and phase-1-transform depending on the input format
@@ -342,6 +344,7 @@
343345 this.childFactories = childFactories;
344346 this._construct();
345347 this._reset( args, env );
 348+ this.phaseEndRank = phaseEndRank;
346349 // FIXME: pass actual title?
347350 this.loopAndDepthCheck = new LoopAndDepthCheck( null );
348351 }
@@ -473,7 +476,6 @@
474477 //console.warn('AsyncTokenTransformManager.transformTokens: ' + JSON.stringify(tokens) );
475478
476479 var res,
477 - phaseEndRank = 2, // XXX: parametrize!
478480 // Prepare a new accumulator, to be used by async children (if any)
479481 localAccum = [],
480482 accum = new TokenAccumulator( this, parentCB ),
@@ -489,26 +491,26 @@
490492
491493 switch ( token.constructor ) {
492494 case String:
493 - res = this._transformToken( token, phaseEndRank, ts.text, cb );
 495+ res = this._transformToken( token, ts.text, cb );
494496 break;
495497 case NlTk:
496 - res = this._transformToken( token, phaseEndRank, ts.newline, cb );
 498+ res = this._transformToken( token, ts.newline, cb );
497499 break;
498500 case TagTk:
499501 case EndTagTk:
500502 case SelfclosingTagTk:
501 - res = this._transformTagToken( token, phaseEndRank, cb );
 503+ res = this._transformTagToken( token, cb );
502504 break;
503505 default:
504506 switch( token.type ) {
505507 case 'COMMENT':
506 - res = this._transformToken( token, phaseEndRank, ts.comment, cb );
 508+ res = this._transformToken( token, ts.comment, cb );
507509 break;
508510 case 'END':
509 - res = this._transformToken( token, phaseEndRank, ts.end, cb );
 511+ res = this._transformToken( token, ts.end, cb );
510512 break;
511513 default:
512 - res = this._transformToken( token, phaseEndRank, ts.martian, cb );
 514+ res = this._transformToken( token, ts.martian, cb );
513515 break;
514516 }
515517 break;
@@ -626,7 +628,7 @@
627629 * @constructor
628630 * @param {Object} environment.
629631 */
630 -function SyncTokenTransformManager ( env, phaseEndRank, inputType ) {
 632+function SyncTokenTransformManager ( env, inputType, phaseEndRank ) {
631633 // both inherited
632634 this._construct();
633635 this.phaseEndRank = phaseEndRank;
@@ -672,27 +674,26 @@
673675
674676 switch( token.constructor ) {
675677 case String:
676 - res = this._transformToken( token, this.phaseEndRank,
677 - ts.text, this.prevToken );
 678+ res = this._transformToken( token, ts.text, this.prevToken );
678679 break;
679680 case NlTk:
680 - res = this._transformToken( token, this.phaseEndRank, ts.newline, this.prevToken );
 681+ res = this._transformToken( token, ts.newline, this.prevToken );
681682 break;
682683 case TagTk:
683684 case EndTagTk:
684685 case SelfclosingTagTk:
685 - res = this._transformTagToken( token, this.phaseEndRank, this.prevToken );
 686+ res = this._transformTagToken( token, this.prevToken );
686687 break;
687688 default:
688689 switch( token.type ) {
689690 case 'COMMENT':
690 - res = this._transformToken( token, this.phaseEndRank, ts.comment, this.prevToken );
 691+ res = this._transformToken( token, ts.comment, this.prevToken );
691692 break;
692693 case 'END':
693 - res = this._transformToken( token, this.phaseEndRank, ts.end, this.prevToken );
 694+ res = this._transformToken( token, ts.end, this.prevToken );
694695 break;
695696 default:
696 - res = this._transformToken( token, this.phaseEndRank, ts.martian, this.prevToken );
 697+ res = this._transformToken( token, ts.martian, this.prevToken );
697698 break;
698699 }
699700 }
Index: trunk/extensions/VisualEditor/modules/parser/parse.js
@@ -49,6 +49,8 @@
5050 // fetch templates from enwiki by default..
5151 wgScriptPath: argv.wgScriptPath,
5252 wgScriptExtension: argv.wgScriptExtension,
 53+ // XXX: add options for this!
 54+ wgUploadPath: 'http://upload.wikimedia.org/wikipedia/commons/thumb/',
5355 fetchTemplates: argv.fetchTemplates,
5456 // enable/disable debug output using this switch
5557 debug: argv.debug,
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js
@@ -399,15 +399,15 @@
400400 return [ target ];
401401 };
402402 ParserFunctions.prototype['pf_fullpagename'] = function ( target, argList, argDict ) {
403 - return [target];
 403+ return target && [target] || ["http://example.com/fixme/"];
404404 };
405405 ParserFunctions.prototype['pf_fullpagenamee'] = function ( target, argList, argDict ) {
406 - return [target];
 406+ return target && [target] || ["http://example.com/fixme/"];
407407 };
408408 // This should be doable with the information in the envirionment
409409 // (this.manager.env) already.
410410 ParserFunctions.prototype['pf_fullurl'] = function ( target, argList, argDict ) {
411 - return [target];
 411+ return target && [target] || ["http://example.com/fixme/"];
412412 };
413413 ParserFunctions.prototype['pf_urlencode'] = function ( target, argList, argDict ) {
414414 this.manager.env.tp( 'urlencode: ' + target );
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.AttributeExpander.js
@@ -42,7 +42,11 @@
4343 * processes the template.
4444 */
4545 AttributeExpander.prototype.onToken = function ( token, frame, cb ) {
46 - if ( token.constructor === TagTk && token.attribs && token.attribs.length ) {
 46+ this.manager.env.dp( 'AttributeExpander.onToken', token );
 47+ if ( token.constructor === TagTk ||
 48+ token.constructor === SelfclosingTagTk &&
 49+ token.attribs &&
 50+ token.attribs.length ) {
4751 var expandData = {
4852 token: token,
4953 cb: cb
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js
@@ -112,8 +112,8 @@
113113 var MD5 = new jshashes.MD5(),
114114 hash = MD5.hex( title.key ),
115115 // TODO: Hackhack.. Move to proper test harness setup!
116 - path = 'http://example.com/images/' +
117 - [ hash[0], hash.substr(0, 2) ].join('/') + '/' + title.key;
 116+ path = [ this.manager.env.wgUploadPath, hash[0],
 117+ hash.substr(0, 2), title.key ].join('/');
118118
119119
120120
@@ -153,8 +153,8 @@
154154 var img = new SelfclosingTagTk( 'img',
155155 [
156156 // FIXME!
157 - new KV( 'height', options.height || '220' ),
158 - new KV( 'width', options.width || '1941' ),
 157+ new KV( 'height', options.height || '120' ),
 158+ new KV( 'width', options.width || '120' ),
159159 new KV( 'src', path ),
160160 new KV( 'alt', options.alt || title.key )
161161 ] );
@@ -217,8 +217,9 @@
218218 };
219219
220220 ExternalLinkHandler.prototype.onUrlLink = function ( token, manager, cb ) {
221 - var href = this.manager.env.sanitizeURI(
222 - this.manager.env.lookupKV( token.attribs, 'href' ).v
 221+ var env = this.manager.env,
 222+ href = env.sanitizeURI(
 223+ env.tokensToString( env.lookupKV( token.attribs, 'href' ).v )
223224 );
224225 if ( this._isImageLink( href ) ) {
225226 return { token: new SelfclosingTagTk( 'img',
@@ -241,9 +242,10 @@
242243
243244 // Bracketed external link
244245 ExternalLinkHandler.prototype.onExtLink = function ( token, manager, cb ) {
245 - var href = this.manager.env.lookupKV( token.attribs, 'href' ).v,
246 - content= this.manager.env.lookupKV( token.attribs, 'content' ).v;
247 - href = this.manager.env.sanitizeURI( href );
 246+ var env = this.manager.env,
 247+ href = env.tokensToString( env.lookupKV( token.attribs, 'href' ).v ),
 248+ content= env.lookupKV( token.attribs, 'content' ).v;
 249+ href = env.sanitizeURI( href );
248250 //console.warn('extlink href: ' + href );
249251 //console.warn( 'content: ' + JSON.stringify( content, null, 2 ) );
250252 // validate the href
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.Title.js
@@ -10,9 +10,10 @@
1111 Title.prototype.makeLink = function () {
1212 // XXX: links always point to the canonical namespace name.
1313 if ( false && this.nskey ) {
14 - return this.env.sanitizeURI( this.env.wgScriptPath + this.nskey + ':' + this.key );
 14+ return this.env.sanitizeURI( this.env.wgScriptPath + '/' +
 15+ this.nskey + ':' + this.key );
1516 } else {
16 - var l = this.env.wgScriptPath,
 17+ var l = this.env.wgScriptPath + '/',
1718 ns = this.ns.getDefaultName();
1819
1920 if ( ns ) {
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.js
@@ -76,7 +76,7 @@
7777
7878
7979 this.tokenPostProcessor = new TokenTransformManager
80 - .SyncTokenTransformManager ( env, inputType );
 80+ .SyncTokenTransformManager ( env, inputType, 3.0 );
8181 this.tokenPostProcessor.listenForTokensFrom ( this.inputPipeline );
8282
8383
@@ -239,7 +239,7 @@
240240 * https://www.mediawiki.org/wiki/Future/Parser_development/Token_stream_transformations
241241 */
242242 var tokenPreProcessor = new TokenTransformManager
243 - .SyncTokenTransformManager ( this.env );
 243+ .SyncTokenTransformManager ( this.env, 'text/wiki', 1 );
244244 tokenPreProcessor.listenForTokensFrom ( wikiTokenizer );
245245
246246 this._addTransformers( 'text/wiki', 'sync01',
@@ -251,7 +251,7 @@
252252 'input': this.makeInputPipeline.bind( this ),
253253 'attributes': this.makeAttributePipeline.bind( this )
254254 },
255 - args, this.env, inputType
 255+ args, this.env, inputType, 2.0
256256 );
257257
258258 // Register template expansion extension
@@ -295,10 +295,10 @@
296296 * See https://www.mediawiki.org/wiki/Future/Parser_development/Token_stream_transformations
297297 */
298298 var tokenPreProcessor = new TokenTransformManager
299 - .SyncTokenTransformManager ( this.env, inputType );
 299+ .SyncTokenTransformManager ( this.env, inputType, 1 );
300300
301301 // XXX: set include flag properly!
302 - //this._addTransformers( inputType, 'sync01', tokenPreProcessor, false );
 302+ this._addTransformers( inputType, 'sync01', tokenPreProcessor, false );
303303
304304 new NoInclude( tokenPreProcessor );
305305
@@ -307,7 +307,7 @@
308308 'input': this.makeInputPipeline.bind( this ),
309309 'attributes': this.makeAttributePipeline.bind( this )
310310 },
311 - args, this.env, inputType
 311+ args, this.env, inputType, 2
312312 );
313313 // Register template expansion extension
314314 this._addTransformers( 'text/wiki', 'async12',
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
@@ -9,7 +9,9 @@
1010 pageCache: {}, // @fixme use something with managed space
1111 debug: false,
1212 trace: false,
13 - wgScriptPath: "http://en.wikipedia.org/w",
 13+ wgScriptPath: "/wiki",
 14+ wgScript: "/wiki/index.php",
 15+ wgUploadPath: "/wiki/images",
1416 wgScriptExtension: ".php",
1517 fetchTemplates: false,
1618 maxDepth: 40

Status & tagging log