Arlolra has uploaded a new change for review.
https://gerrit.wikimedia.org/r/207012
Change subject: Refactor parsoidConfig uses to remove unfrozen flag
......................................................................
Refactor parsoidConfig uses to remove unfrozen flag
Change-Id: I6d8c831615ad6261b9059a4b1f16b7d919a947f0
---
M lib/mediawiki.ParsoidConfig.js
M lib/mediawiki.Util.js
M tests/client/client.js
M tests/fetch-wt.js
M tests/parse.js
M tests/parserTests.js
M tests/roundtrip-test.js
7 files changed, 126 insertions(+), 137 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid
refs/changes/12/207012/1
diff --git a/lib/mediawiki.ParsoidConfig.js b/lib/mediawiki.ParsoidConfig.js
index dc6ed1f..c8231b2 100644
--- a/lib/mediawiki.ParsoidConfig.js
+++ b/lib/mediawiki.ParsoidConfig.js
@@ -24,47 +24,43 @@
* @param {ParsoidConfig} localSettings.setup.opts The setup function is
passed the object under construction so it can extend the config directly.
* @param {Object} options Any options we want to set over the defaults. Will
not overwrite things set by the localSettings.setup function. See the class
properties for more information.
*/
-function ParsoidConfig( localSettings, options ) {
+function ParsoidConfig(localSettings, options) {
this.interwikiMap = new Map();
this.reverseIWMap = new Map();
this.apiProxies = new Map();
this.interwikiRegexp = "";
- if ( localSettings && localSettings.setup ) {
- localSettings.setup( this );
+ // Load WMF wikis first, so they can be overwritten in the setup.
+ if (this.loadWMF) {
+ this.initInterwikiMap();
+ }
+
+ if (localSettings && localSettings.setup) {
+ localSettings.setup(this);
}
// Don't freak out!
// This happily overwrites inherited properties.
if (options) {
- Object.assign( this, options );
- }
-
- if ( this.loadWMF ) {
- this.initInterwikiMap();
+ Object.assign(this, options);
}
// SSS FIXME: Hardcoded right now, but need a generic registration
mechanism
// for native handlers
this.nativeExtensions = {
- cite: new Cite()
+ cite: new Cite(),
};
// Permissive CORS headers as Parsoid is full idempotent currently
this.allowCORS = '*';
// Timer that reports metrics to statsd
- if ( this.useDefaultPerformanceTimer ) {
- this.performanceTimer = new Util.StatsD( this.txstatsdHost,
this.txstatsdPort );
+ if (this.useDefaultPerformanceTimer) {
+ this.performanceTimer = new Util.StatsD(this.txstatsdHost,
this.txstatsdPort);
}
// ParsoidConfig is used across requests. Freeze it to avoid mutation.
- // The option to remain unfrozen is for specific use cases (think
- // parserTests) but we should work towards eliminating those (with
- // the possible introduction of a clone() method).
- if (!this.unfrozen) {
- JSUtils.deepFreeze(this);
- }
+ JSUtils.deepFreeze(this);
}
/**
@@ -267,7 +263,6 @@
*/
ParsoidConfig.prototype.initInterwikiMap = function() {
var insertInMaps = function(proxyURI, site) {
- // Avoid overwriting those already set in localsettings setup.
if (!this.interwikiMap.has(site.dbname)) {
var url = site.url;
var proxyHeaders;
diff --git a/lib/mediawiki.Util.js b/lib/mediawiki.Util.js
index 6e744e5..2712a8b 100644
--- a/lib/mediawiki.Util.js
+++ b/lib/mediawiki.Util.js
@@ -183,34 +183,40 @@
* @param {Object} opts The options object to use for setting the debug
flags.
* @returns {Object} The modified object.
*/
-
- setTemplatingAndProcessingFlags: function(obj, opts) {
-
- ['fetchConfig', 'fetchTemplates', 'rtTestMode',
'addHTMLTemplateParameters'].forEach(function(c) {
+ setTemplatingAndProcessingFlags: function(parsoidConfig, opts) {
+ [
+ 'fetchConfig',
+ 'fetchTemplates',
+ 'rtTestMode',
+ 'addHTMLTemplateParameters'
+ ].forEach(function(c) {
if (opts[c] !== undefined) {
- obj[c] = Util.booleanOption( opts[c] );
+ parsoidConfig[c] = Util.booleanOption(opts[c]);
}
});
if (opts.usephppreprocessor !== undefined) {
- obj.usePHPPreProcessor = obj.fetchTemplates &&
Util.booleanOption( opts.usephppreprocessor );
+ parsoidConfig.usePHPPreProcessor =
parsoidConfig.fetchTemplates &&
+ Util.booleanOption(opts.usephppreprocessor);
}
if (opts.maxDepth !== undefined) {
- obj.maxDepth = typeof (opts.maxdepth) === 'number' ?
opts.maxdepth : obj.maxDepth;
+ parsoidConfig.maxDepth = typeof (opts.maxdepth) ===
'number' ?
+ opts.maxdepth : parsoidConfig.maxDepth;
}
if (opts.dp !== undefined) {
- obj.storeDataParsoid = Util.booleanOption( opts.dp );
+ parsoidConfig.storeDataParsoid =
Util.booleanOption(opts.dp);
}
if (opts.apiURL) {
- obj.setInterwiki( 'customwiki', opts.apiURL );
+ parsoidConfig.setInterwiki('customwiki', opts.apiURL);
}
if (opts.addHTMLTemplateParameters !== undefined) {
- obj.addHTMLTemplateParameters =
Util.booleanOption(opts.addHTMLTemplateParameters);
+ parsoidConfig.addHTMLTemplateParameters =
+
Util.booleanOption(opts.addHTMLTemplateParameters);
}
if (opts.lint) {
- obj.linting = true;
- obj.linterAPI = null;
+ parsoidConfig.linting = true;
+ parsoidConfig.linterAPI = null;
}
- return obj;
+ return parsoidConfig;
},
/**
diff --git a/tests/client/client.js b/tests/client/client.js
index 1b8b8c3..f12404a 100755
--- a/tests/client/client.js
+++ b/tests/client/client.js
@@ -222,16 +222,16 @@
callbackOmnibus('start');
};
- // Enable heap dumps in /tmp on kill -USR2.
- // See https://github.com/bnoordhuis/node-heapdump/
- // For node 0.6/0.8: npm install [email protected]
- // For 0.10: npm install heapdump
- process.on('SIGUSR2', function() {
- var heapdump = require('heapdump');
- console.error('SIGUSR2 received! Writing snapshot.');
- process.chdir('/tmp');
- heapdump.writeSnapshot();
- });
+ // Enable heap dumps in /tmp on kill -USR2.
+ // See https://github.com/bnoordhuis/node-heapdump/
+ // For node 0.6/0.8: npm install [email protected]
+ // For 0.10: npm install heapdump
+ process.on('SIGUSR2', function() {
+ var heapdump = require('heapdump');
+ console.error('SIGUSR2 received! Writing snapshot.');
+ process.chdir('/tmp');
+ heapdump.writeSnapshot();
+ });
if ( !config.parsoidURL ) {
// If no Parsoid server was passed, start our own
diff --git a/tests/fetch-wt.js b/tests/fetch-wt.js
index 9fe5f5e..a835226 100755
--- a/tests/fetch-wt.js
+++ b/tests/fetch-wt.js
@@ -13,57 +13,41 @@
var TemplateRequest =
require('../lib/mediawiki.ApiRequest.js').TemplateRequest;
var ParsoidConfig = require('../lib/mediawiki.ParsoidConfig').ParsoidConfig;
var MWParserEnvironment =
require('../lib/mediawiki.parser.environment.js').MWParserEnvironment;
+var Util = require('../lib/mediawiki.Util.js').Util;
-var fetch = function(page, revid, cb, options) {
- cb = typeof cb === 'function' ? cb : function() {};
-
- var envCb = function( err, env ) {
- env.errCB = function( error ) {
- cb( error, env, [] );
- };
- if ( err !== null ) {
- env.errCB( err );
- return;
- }
-
- var target = page ? env.resolveTitle( env.normalizeTitle(
env.page.name ), '' ) : null;
- var tpr = new TemplateRequest( env, target, revid );
-
- tpr.once( 'src', function( err, src_and_metadata ) {
- if ( err ) {
- cb( err, env, [] );
- } else {
- env.setPageSrcInfo( src_and_metadata );
-
- // ok, got it
- if ( options.output ) {
- fs.writeFileSync( options.output,
env.page.src, 'utf8');
- } else {
- console.log( env.page.src );
- }
- }
- } );
- };
-
+var fetch = function(page, revid, options) {
var prefix = options.prefix || null;
- if ( options.apiURL ) {
+ if (options.apiURL) {
prefix = 'customwiki';
}
- options.setup = function(parsoidConfig) {
- if (options.apiURL) {
- parsoidConfig.setInterwiki('customwiki',
options.apiURL);
- }
+ var setup = function(parsoidConfig) {
+ Util.setTemplatingAndProcessingFlags(parsoidConfig, options);
};
- var parsoidConfig = new ParsoidConfig(options, { defaultWiki: prefix });
+ var parsoidConfig = new ParsoidConfig(
+ { setup: setup },
+ { defaultWiki: prefix }
+ );
- MWParserEnvironment.getParserEnv( parsoidConfig, null, {
+ var env;
+ MWParserEnvironment.getParserEnv(parsoidConfig, null, {
prefix: prefix,
pageName: page
- }, envCb );
+ }).then(function(_env) {
+ env = _env;
+ var target = page ?
+ env.resolveTitle(env.normalizeTitle(env.page.name), '')
: null;
+ return TemplateRequest.setPageSrcInfo(env, target, revid);
+ }).then(function() {
+ if (options.output) {
+ fs.writeFileSync(options.output, env.page.src, 'utf8');
+ } else {
+ console.log(env.page.src);
+ }
+ }).done();
};
var usage = 'Usage: $0 [options] <page-title or rev-id>\n' +
@@ -127,4 +111,4 @@
return;
}
-fetch(title, revid, null, argv);
+fetch(title, revid, argv);
diff --git a/tests/parse.js b/tests/parse.js
index aa64197..3f99cb1 100755
--- a/tests/parse.js
+++ b/tests/parse.js
@@ -281,47 +281,54 @@
var argv = opts.argv;
- if ( Util.booleanOption( argv.help ) ) {
+ if (Util.booleanOption(argv.help)) {
opts.showHelp();
return;
}
// Because selser builds on html2wt serialization,
// the html2wt flag should be automatically set when selser is
set.
- if ( argv.selser ) {
+ if (argv.selser) {
argv.html2wt = true;
}
// Default conversion mode
- if ( !argv.html2wt && !argv.wt2wt && !argv.html2html ) {
+ if (!argv.html2wt && !argv.wt2wt && !argv.html2html) {
argv.wt2html = true;
}
var prefix = argv.prefix || null;
- if ( argv.apiURL ) {
+ if (argv.apiURL) {
prefix = 'customwiki';
}
var local = null;
- if ( Util.booleanOption( argv.config ) ) {
- var p = ( typeof ( argv.config ) === 'string' ) ?
- path.resolve( '.', argv.config) :
- path.resolve( __dirname,
'../api/localsettings.js' );
- local = require( p );
+ if (Util.booleanOption(argv.config)) {
+ var p = (typeof (argv.config) === 'string') ?
+ path.resolve('.', argv.config) :
+ path.resolve(__dirname,
'../api/localsettings.js');
+ local = require(p);
}
- var parsoidConfig = new ParsoidConfig(local, {
- defaultWiki: prefix,
- unfrozen: true // FIXME: refactor the mutations below
- });
- Util.setTemplatingAndProcessingFlags( parsoidConfig, argv );
- Util.setDebuggingFlags( parsoidConfig, argv );
- return parse( null, argv, parsoidConfig, prefix
).then(function( res ) {
+ var setup = function(parsoidConfig) {
+ if (local && local.setup) {
+ local.setup(parsoidConfig);
+ }
+ Util.setTemplatingAndProcessingFlags(parsoidConfig,
argv);
+ Util.setDebuggingFlags(parsoidConfig, argv);
+ };
+
+ var parsoidConfig = new ParsoidConfig(
+ { setup: setup },
+ { defaultWiki: prefix }
+ );
+
+ return parse(null, argv, parsoidConfig,
prefix).then(function(res) {
var stdout = process.stdout;
- stdout.write( res.out );
- if ( res.trailingNL && stdout.isTTY ) {
- stdout.write( "\n" );
+ stdout.write(res.out);
+ if (res.trailingNL && stdout.isTTY) {
+ stdout.write('\n');
}
}).done();
}());
diff --git a/tests/parserTests.js b/tests/parserTests.js
index 0944c6b..e6b1a56 100755
--- a/tests/parserTests.js
+++ b/tests/parserTests.js
@@ -26,7 +26,6 @@
var ParsoidLogger = require('../lib/ParsoidLogger.js').ParsoidLogger;
var PEG = require('pegjs');
var Util = require('../lib/mediawiki.Util.js').Util;
-var JSUtils = require('../lib/jsutils.js').JSUtils;
var Diff = require('../lib/mediawiki.Diff.js').Diff;
// Fetch up some of our wacky parser bits...
@@ -1657,34 +1656,32 @@
options.expandExtensions = true;
- // TODO: Refactor to eliminate the mutations below.
- options.unfrozen = true;
+ var setup = function(parsoidConfig) {
+ // Set tracing and debugging before the env. object is
+ // constructed since tracing backends are registered there.
+ // (except for the --quiet option where the backends are
+ // overridden here).
+ Util.setDebuggingFlags(parsoidConfig, options);
+ Util.setTemplatingAndProcessingFlags(parsoidConfig, options);
- var parsoidConfig = new ParsoidConfig( null, options );
- parsoidConfig.interwikiMap.forEach(function( val, key ) {
- parsoidConfig.setInterwiki(key, mockAPIServerURL);
- });
+ // Send all requests to the mock API server.
+ parsoidConfig.interwikiMap.forEach(function(val, key) {
+ parsoidConfig.setInterwiki(key, mockAPIServerURL);
+ });
- // This isn't part of the sitematrix but the
- // "Check noCommafy in formatNum" test depends on it.
- parsoidConfig.setInterwiki('be-taraskwiki', mockAPIServerURL);
+ // This isn't part of the sitematrix but the
+ // "Check noCommafy in formatNum" test depends on it.
+ parsoidConfig.setInterwiki('be-taraskwiki', mockAPIServerURL);
+ };
- // Set tracing and debugging before the env. object is
- // constructed since tracing backends are registered there.
- // (except for the --quiet option where the backends are
- // overridden here).
- Util.setDebuggingFlags(parsoidConfig, options);
- Util.setTemplatingAndProcessingFlags( parsoidConfig, options );
-
- parsoidConfig.rtTestMode = options.rtTestMode;
-
- // See the todo above.
- JSUtils.deepFreeze(parsoidConfig);
+ var parsoidConfig = new ParsoidConfig({ setup: setup }, options);
// Create a new parser environment
- MWParserEnvironment.getParserEnv( parsoidConfig, null, { prefix:
'enwiki' }, function( err, env ) {
- // For posterity: err will never be non-null here, because we
expect the WikiConfig
- // to be basically empty, since the parserTests environment is
very bare.
+ MWParserEnvironment.getParserEnv(parsoidConfig, null, { prefix:
'enwiki' },
+ function(err, env) {
+ // For posterity: err will never be non-null here, because we
expect
+ // the WikiConfig to be basically empty, since the parserTests
+ // environment is very bare.
this.env = env;
if (booleanOption( options.quiet )) {
diff --git a/tests/roundtrip-test.js b/tests/roundtrip-test.js
index 7c64393..2c518ad 100755
--- a/tests/roundtrip-test.js
+++ b/tests/roundtrip-test.js
@@ -582,19 +582,19 @@
// Returns a Promise for a formatted string. `cb` is optional.
function runTests(title, options, formatter, cb) {
- // options are ParsoidConfig options if module.parent, otherwise they
- // are CLI options (so use the Util.set* helpers to process them)
- var parsoidConfig = new ParsoidConfig(module.parent ? options : null, {
- unfrozen: true // FIXME: refactor the mutations below
- });
- if (!module.parent) {
- // only process CLI flags if we're running as a CLI program.
- Util.setTemplatingAndProcessingFlags(parsoidConfig, options);
- Util.setDebuggingFlags(parsoidConfig, options);
- }
- if (options.apiURL) {
- parsoidConfig.setInterwiki(options.prefix || 'localhost',
options.apiURL);
- }
+ var setup = function(parsoidConfig) {
+ // options are ParsoidConfig options if module.parent,
otherwise they
+ // are CLI options (so use the Util.set* helpers to process
them)
+ if (module.parent) {
+ if (options && options.setup) {
+ options.setup(parsoidConfig);
+ }
+ } else {
+ Util.setTemplatingAndProcessingFlags(parsoidConfig,
options);
+ Util.setDebuggingFlags(parsoidConfig, options);
+ }
+ };
+ var parsoidConfig = new ParsoidConfig({ setup: setup });
var err, domain, prefix;
if (options.prefix) {
// If prefix is present, use that.
--
To view, visit https://gerrit.wikimedia.org/r/207012
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6d8c831615ad6261b9059a4b1f16b7d919a947f0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits