| 1 |
#!/usr/bin/env node |
| 2 |
|
| 3 |
'use strict'; |
| 4 |
|
| 5 |
require('../core-upgrade.js'); |
| 6 |
|
| 7 |
const colors = require('colors'); |
| 8 |
const fs = require('pn/fs'); |
| 9 |
const path = require('path'); |
| 10 |
const yargs = require('yargs'); |
| 11 |
|
| 12 |
const { ApiRequest, DoesNotExistError } = require('../lib/mw/ApiRequest.js'); |
| 13 |
const { Diff } = require('../lib/utils/Diff.js'); |
| 14 |
const { DOMDataUtils } = require('../lib/utils/DOMDataUtils.js'); |
| 15 |
const { DOMTraverser } = require('../lib/utils/DOMTraverser.js'); |
| 16 |
const { DOMUtils } = require('../lib/utils/DOMUtils.js'); |
| 17 |
const { MWParserEnvironment } = require('../lib/config/MWParserEnvironment.js'); |
| 18 |
const { ParsoidConfig } = require('../lib/config/ParsoidConfig.js'); |
| 19 |
const Promise = require('../lib/utils/promise.js'); |
| 20 |
const { TemplateRequest } = require('../lib/mw/ApiRequest.js'); |
| 21 |
const { Util } = require('../lib/utils/Util.js'); |
| 22 |
const { ScriptUtils } = require('../tools/ScriptUtils.js'); |
| 23 |
|
| 24 |
const jsonFormat = function(error, domain, title, lang, options, results) { |
| 25 |
if (error) { |
| 26 |
return { error: error.stack || error.toString() }; |
| 27 |
} |
| 28 |
const p = Diff.patchDiff(results.php, results.parsoid); |
| 29 |
return { patch: p }; |
| 30 |
}; |
| 31 |
|
| 32 |
const plainFormat = function(error, domain, title, lang, options, results) { |
| 33 |
if (error) { |
| 34 |
return error.stack || error.toString(); |
| 35 |
} |
| 36 |
const article = `${ domain } ${ title } ${ lang || '' }`; |
| 37 |
const diff = Diff.colorDiff(results.php, results.parsoid, { |
| 38 |
context: 1, |
| 39 |
noColor: (colors.mode === 'none'), |
| 40 |
diffCount: true, |
| 41 |
}); |
| 42 |
if (diff.count === 0) { |
| 43 |
return ''; |
| 44 |
} |
| 45 |
return `== ${ article } ==\n${ diff.output }\n${ diff.count } different words found.\n`; |
| 46 |
}; |
| 47 |
|
| 48 |
const xmlFormat = function(error, domain, title, lang, options, results) { |
| 49 |
const article = `${ domain } ${ title } ${ lang || '' }`; |
| 50 |
let output = '<testsuites>\n'; |
| 51 |
output += `<testsuite name="Variant ${ Util.escapeHtml(article) }">\n`; |
| 52 |
output += `<testcase name="revision ${ results.revid }">\n`; |
| 53 |
if (error) { |
| 54 |
output += '<error type="parserFailedToFinish">'; |
| 55 |
output += Util.escapeHtml(error.stack || error.toString()); |
| 56 |
output += '</error>'; |
| 57 |
} else if (results.php !== results.parsoid) { |
| 58 |
output += '<failure type="diff">\n<diff class="html">\n'; |
| 59 |
output += Diff.colorDiff(results.php, results.parsoid, { |
| 60 |
context: 1, |
| 61 |
html: true, |
| 62 |
separator: '</diff></failure>\n' + |
| 63 |
'<failure type="diff"><diff class="html">', |
| 64 |
}); |
| 65 |
output += '\n</diff>\n</failure>\n'; |
| 66 |
} |
| 67 |
output += '</testcase>\n'; |
| 68 |
output += '</testsuite>\n'; |
| 69 |
output += '</testsuites>\n'; |
| 70 |
return output; |
| 71 |
}; |
| 72 |
|
| 73 |
const silentFormat = function(error, domain, title, lang, options, results) { |
| 74 |
return ''; |
| 75 |
}; |
| 76 |
|
| 77 |
class PHPVariantRequest extends ApiRequest { |
| 78 |
constructor(env, title, variant, revid) { |
| 79 |
super(env, title); |
| 80 |
this.reqType = "Variant Parse"; |
| 81 |
|
| 82 |
const apiargs = { |
| 83 |
format: 'json', |
| 84 |
action: 'parse', |
| 85 |
page: title, |
| 86 |
prop: 'text|revid|displaytitle', |
| 87 |
uselang: 'content', |
| 88 |
wrapoutputclass: '', |
| 89 |
disableeditsection: 'true', |
| 90 |
disabletoc: 'true', |
| 91 |
disablelimitreport: 'true' |
| 92 |
}; |
| 93 |
if (revid) { |
| 94 |
// The parameters `page` and `oldid` can't be used together |
| 95 |
apiargs.page = undefined; |
| 96 |
apiargs.oldid = revid; |
| 97 |
} |
| 98 |
// This argument to the API is not documented! Except in |
| 99 |
// https://phabricator.wikimedia.org/T44356#439479 and |
| 100 |
// https://phabricator.wikimedia.org/T34906#381101 |
| 101 |
if (variant) { |
| 102 |
apiargs.variant = variant; |
| 103 |
} |
| 104 |
|
| 105 |
const uri = env.conf.wiki.apiURI; |
| 106 |
this.requestOptions = { |
| 107 |
uri, |
| 108 |
method: 'POST', |
| 109 |
form: apiargs, // The API arguments |
| 110 |
followRedirect: true, |
| 111 |
timeout: env.conf.parsoid.timeouts.mwApi.extParse, |
| 112 |
}; |
| 113 |
|
| 114 |
this.request(this.requestOptions); |
| 115 |
} |
| 116 |
|
| 117 |
_handleJSON(error, data) { |
| 118 |
if (!error && !(data && data.parse)) { |
| 119 |
error = this._errorObj(data, this.text, 'Missing data.parse.'); |
| 120 |
} |
| 121 |
|
| 122 |
if (error) { |
| 123 |
this.env.log("error", error); |
| 124 |
this._processListeners(error, ''); |
| 125 |
} else { |
| 126 |
this._processListeners(error, data.parse); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
_errorObj(data, requestStr, defaultMsg) { |
| 131 |
if (data && data.error && data.error.code === 'missingtitle') { |
| 132 |
return new DoesNotExistError(this.title); |
| 133 |
} |
| 134 |
return super._errorObj(data, requestStr, defaultMsg); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
const phpFetch = Promise.async(function *(env, title, revid) { |
| 139 |
const parse = yield new Promise((resolve, reject) => { |
| 140 |
const req = new PHPVariantRequest( |
| 141 |
env, title, env.htmlVariantLanguage, revid |
| 142 |
); |
| 143 |
req.once('src', (err, src) => err ? reject(err) : resolve(src)); |
| 144 |
}); |
| 145 |
const document = DOMUtils.parseHTML(parse.text['*']); |
| 146 |
const displaytitle = parse.displaytitle; |
| 147 |
revid = parse.revid; |
| 148 |
return { |
| 149 |
document, |
| 150 |
revid, |
| 151 |
displaytitle |
| 152 |
}; |
| 153 |
}); |
| 154 |
|
| 155 |
const parsoidFetch = Promise.async(function *(env, title, options) { |
| 156 |
if (!options.useServer) { |
| 157 |
yield TemplateRequest.setPageSrcInfo(env, title, options.oldid); |
| 158 |
const revision = env.page.meta.revision; |
| 159 |
const handler = env.getContentHandler(revision.contentmodel); |
| 160 |
const document = yield handler.toHTML(env); |
| 161 |
return { |
| 162 |
document, |
| 163 |
revid: revision.revid, |
| 164 |
displaytitle: document.title, |
| 165 |
}; |
| 166 |
} |
| 167 |
const domain = options.domain; |
| 168 |
let uri = options.uri; |
| 169 |
// Make sure the Parsoid URI ends with `/` |
| 170 |
if (!/\/$/.test(uri)) { |
| 171 |
uri += '/'; |
| 172 |
} |
| 173 |
uri += `${ domain }/v3/page/html/${ encodeURIComponent(title) }`; |
| 174 |
if (options.oldid) { |
| 175 |
uri += `/${ options.oldid }`; |
| 176 |
} |
| 177 |
const resp = yield ScriptUtils.retryingHTTPRequest(10, { |
| 178 |
method: 'GET', |
| 179 |
uri, |
| 180 |
headers: { |
| 181 |
'User-Agent': env.userAgent, |
| 182 |
'Accept-Language': env.htmlVariantLanguage, |
| 183 |
} |
| 184 |
}); |
| 185 |
// We may have been redirected to the latest revision. Record oldid. |
| 186 |
const res = resp[0]; |
| 187 |
const body = resp[1]; |
| 188 |
if (res.statusCode !== 200) { |
| 189 |
throw new Error(`Can\'t fetch Parsoid source: ${ uri }`); |
| 190 |
} |
| 191 |
const oldid = res.request.path.replace(/^(.*)\//, ''); |
| 192 |
const document = DOMUtils.parseHTML(body); |
| 193 |
return { |
| 194 |
document, |
| 195 |
revid: oldid, |
| 196 |
displaytitle: document.title, |
| 197 |
}; |
| 198 |
}); |
| 199 |
|
| 200 |
const hrefToTitle = function(href) { |
| 201 |
return Util.decodeURIComponent(href.replace(/^(\.\.?|\/wiki)\//, '')) |
| 202 |
.replace(/_/g, ' '); |
| 203 |
}; |
| 204 |
|
| 205 |
const nodeHrefToTitle = function(node, suppressCategory) { |
| 206 |
const href = node && node.hasAttribute('href') && node.getAttribute('href'); |
| 207 |
if (!href) { |
| 208 |
return null; |
| 209 |
} |
| 210 |
const title = hrefToTitle(href); |
| 211 |
if (suppressCategory) { |
| 212 |
const categoryMatch = title.match(/^([^:]+)[:]/); |
| 213 |
if (categoryMatch) { |
| 214 |
return null; /* skip it */ |
| 215 |
} |
| 216 |
} |
| 217 |
return title; |
| 218 |
}; |
| 219 |
|
| 220 |
/** |
| 221 |
* Pull a list of local titles from wikilinks in a Parsoid HTML document. |
| 222 |
* |
| 223 |
* @param env |
| 224 |
* @param document |
| 225 |
*/ |
| 226 |
const spiderDocument = function(env, document) { |
| 227 |
const redirect = document.querySelector('link[rel~="mw:PageProp/redirect"]'); |
| 228 |
const nodes = redirect ? [ redirect ] : |
| 229 |
Array.from(document.querySelectorAll('a[rel~="mw:WikiLink"][href]')); |
| 230 |
return new Set( |
| 231 |
nodes.map(node => nodeHrefToTitle(node, true)).filter(t => t !== null) |
| 232 |
); |
| 233 |
}; |
| 234 |
|
| 235 |
/** |
| 236 |
* Pull "just the text" from an HTML document, normalizing whitespace |
| 237 |
* differences and suppressing places where Parsoid and PHP output |
| 238 |
* deliberately differs. |
| 239 |
* |
| 240 |
* @param env |
| 241 |
* @param document |
| 242 |
*/ |
| 243 |
const extractText = function(env, document) { |
| 244 |
var dt = new DOMTraverser(); |
| 245 |
var sep = ''; |
| 246 |
var buf = ''; |
| 247 |
/* We normalize all whitespace in text nodes to a single space. We |
| 248 |
* do insert newlines in the output, but only to delimit block |
| 249 |
* elements. Even there, we are careful never to emit two newlines |
| 250 |
* in a row, or whitespace before or after a newline. */ |
| 251 |
const addSep = (s) => { |
| 252 |
if (s === '') { |
| 253 |
return; |
| 254 |
} |
| 255 |
if (/\n/.test(s)) { |
| 256 |
sep = '\n'; return; |
| 257 |
} |
| 258 |
if (sep === '\n') { |
| 259 |
return; |
| 260 |
} |
| 261 |
sep = ' '; |
| 262 |
}; |
| 263 |
const emit = (s) => { |
| 264 |
if (s !== '') { |
| 265 |
buf += sep; buf += s; sep = ''; |
| 266 |
} |
| 267 |
}; |
| 268 |
dt.addHandler('#text', (node, env2, atTopLevel, tplInfo) => { |
| 269 |
const v = node.nodeValue.replace(/\s+/g, ' '); |
| 270 |
const m = /^(\s*)(.*?)(\s*)$/.exec(v); |
| 271 |
addSep(m[1]); |
| 272 |
emit(m[2]); |
| 273 |
addSep(m[3]); |
| 274 |
return true; |
| 275 |
}); |
| 276 |
dt.addHandler('div', (node) => { |
| 277 |
if (node.classList.contains('magnify') && |
| 278 |
node.parentNode && |
| 279 |
node.parentNode.classList.contains('thumbcaption')) { |
| 280 |
// Skip the "magnify" link, which PHP has and Parsoid doesn't. |
| 281 |
return node.nextSibling; |
| 282 |
} |
| 283 |
return true; |
| 284 |
}); |
| 285 |
/* These are the block elements which we delimit with newlines (aka, |
| 286 |
* we ensure they start on a line of their own). */ |
| 287 |
var forceBreak = () => { |
| 288 |
addSep('\n'); return true; |
| 289 |
}; |
| 290 |
for (const el of ['p','li','div','table','tr','h1','h2','h3','h4','h5','h6','figure', 'figcaption']) { |
| 291 |
dt.addHandler(el, forceBreak); |
| 292 |
} |
| 293 |
dt.addHandler('div', (node) => { |
| 294 |
if (node.classList.contains('thumbcaption')) { |
| 295 |
// <figcaption> (Parsoid) is marked as forceBreak, |
| 296 |
// so thumbcaption (PHP) should be, too. |
| 297 |
forceBreak(); |
| 298 |
} |
| 299 |
return true; |
| 300 |
}); |
| 301 |
/* Separate table columns with spaces */ |
| 302 |
dt.addHandler('td', () => { |
| 303 |
addSep(' '); return true; |
| 304 |
}); |
| 305 |
/* Suppress reference numbers and linkback text */ |
| 306 |
dt.addHandler('sup', (node) => { |
| 307 |
if ( |
| 308 |
node.classList.contains('reference') /* PHP */ || |
| 309 |
node.classList.contains('mw-ref') /* Parsoid */ |
| 310 |
) { |
| 311 |
return node.nextSibling; // Skip contents of this node |
| 312 |
} |
| 313 |
return true; |
| 314 |
}); |
| 315 |
dt.addHandler('span', (node) => { |
| 316 |
if ( |
| 317 |
node.classList.contains('mw-cite-backlink') || |
| 318 |
/\bmw:referencedBy\b/.test(node.getAttribute('rel') || '') |
| 319 |
) { |
| 320 |
return node.nextSibling; // Skip contents of this node |
| 321 |
} |
| 322 |
return true; |
| 323 |
}); |
| 324 |
dt.addHandler('figcaption', (node) => { |
| 325 |
/* Captions are suppressed in PHP for: |
| 326 |
* figure[typeof~="mw:File/Frameless"], figure[typeof~="mw:File"] |
| 327 |
* See Note 5 of https://www.mediawiki.org/wiki/Specs/HTML/1.7.0#Images |
| 328 |
*/ |
| 329 |
if (DOMDataUtils.hasTypeOf(node.parentNode, 'mw:File/Frameless') || |
| 330 |
DOMDataUtils.hasTypeOf(node.parentNode, 'mw:File')) { |
| 331 |
// Skip caption contents, since they don't appear in PHP output. |
| 332 |
return node.nextSibling; |
| 333 |
} |
| 334 |
return true; |
| 335 |
}); |
| 336 |
/* Show the targets of wikilinks, since the titles should be |
| 337 |
* language-converted too. */ |
| 338 |
dt.addHandler('a', (node) => { |
| 339 |
const rel = node.getAttribute('rel') || ''; |
| 340 |
if (/\bmw:referencedBy\b/.test(rel)) { |
| 341 |
// skip reference linkback |
| 342 |
return node.nextSibling; |
| 343 |
} |
| 344 |
let href = node.getAttribute('href') || ''; |
| 345 |
// Rewrite red links as normal links |
| 346 |
let m = /^\/w\/index\.php\?title=(.*?)&.*redlink=1$/.exec(href); |
| 347 |
if (m) { |
| 348 |
href = `/wiki/${ m[1] }`; |
| 349 |
} |
| 350 |
// Local links to this page, or self-links |
| 351 |
m = /^#/.test(href); |
| 352 |
if (m || node.classList.contains('mw-selflink')) { |
| 353 |
const title = encodeURIComponent(env.page.name); |
| 354 |
href = `/wiki/${ title }${ href }`; |
| 355 |
} |
| 356 |
// Now look for wiki links |
| 357 |
if (node.classList.contains('external')) { |
| 358 |
return true; |
| 359 |
} |
| 360 |
if (/^(\.\.?|\/wiki)\//.test(href)) { |
| 361 |
const title = hrefToTitle(href); |
| 362 |
addSep(' '); |
| 363 |
emit(`[${ title }]`); |
| 364 |
addSep(' '); |
| 365 |
} |
| 366 |
return true; |
| 367 |
}); |
| 368 |
dt.addHandler('link', (node) => { |
| 369 |
const rel = node.getAttribute('rel') || ''; |
| 370 |
if (/\bmw:PageProp\/redirect\b/.test(rel)) { |
| 371 |
// Given Parsoid output, emulate PHP output for redirects. |
| 372 |
forceBreak(); |
| 373 |
emit('Redirect to:'); |
| 374 |
forceBreak(); |
| 375 |
const title = nodeHrefToTitle(node); |
| 376 |
emit(`[${ title }]`); |
| 377 |
addSep(' '); |
| 378 |
emit(title); |
| 379 |
return node.nextSibling; |
| 380 |
} |
| 381 |
return true; |
| 382 |
}); |
| 383 |
dt.traverse(document.body); |
| 384 |
return buf; |
| 385 |
}; |
| 386 |
|
| 387 |
// Wrap an asynchronous function in code to record/replay network requests |
| 388 |
const nocksWrap = function(f) { |
| 389 |
return Promise.async(function *(domain, title, lang, options, formatter) { |
| 390 |
let nock, dir, nocksFile; |
| 391 |
if (options.record || options.replay) { |
| 392 |
dir = path.resolve(__dirname, '../nocks/'); |
| 393 |
if (!(yield fs.exists(dir))) { |
| 394 |
yield fs.mkdir(dir); |
| 395 |
} |
| 396 |
dir = `${ dir }/${ domain }`; |
| 397 |
if (!(yield fs.exists(dir))) { |
| 398 |
yield fs.mkdir(dir); |
| 399 |
} |
| 400 |
nocksFile = `${ dir }/lc-${ encodeURIComponent(title) }-${ lang }.js`; |
| 401 |
if (options.record) { |
| 402 |
nock = require('nock'); |
| 403 |
nock.recorder.rec({ dont_print: true }); |
| 404 |
} else { |
| 405 |
require(nocksFile); |
| 406 |
} |
| 407 |
} |
| 408 |
try { |
| 409 |
return (yield f(domain, title, lang, options, formatter)); |
| 410 |
} finally { |
| 411 |
if (options.record) { |
| 412 |
const nockCalls = nock.recorder.play(); |
| 413 |
yield fs.writeFile( |
| 414 |
nocksFile, |
| 415 |
`'use strict';\nlet nock = require('nock');\n${ nockCalls.join('\n') }`, |
| 416 |
'utf8' |
| 417 |
); |
| 418 |
nock.recorder.clear(); |
| 419 |
nock.restore(); |
| 420 |
} |
| 421 |
} |
| 422 |
}); |
| 423 |
}; |
| 424 |
|
| 425 |
const runTest = nocksWrap(Promise.async(function *(domain, title, lang, options, formatter) { |
| 426 |
// Step 0: Configuration & setup |
| 427 |
const parsoidOptions = { |
| 428 |
loadWMF: true, |
| 429 |
}; |
| 430 |
const envOptions = { |
| 431 |
domain, |
| 432 |
pageName: title, |
| 433 |
userAgent: 'LangConvTest', |
| 434 |
wtVariantLanguage: options.sourceVariant || null, |
| 435 |
htmlVariantLanguage: lang || null, |
| 436 |
logLevels: options.verbose ? undefined : ["fatal", "error", "warn"], |
| 437 |
}; |
| 438 |
ScriptUtils.setTemplatingAndProcessingFlags(parsoidOptions, options); |
| 439 |
ScriptUtils.setDebuggingFlags(parsoidOptions, options); |
| 440 |
ScriptUtils.setColorFlags(options); |
| 441 |
|
| 442 |
const parsoidConfig = new ParsoidConfig(null, parsoidOptions); |
| 443 |
const env = yield MWParserEnvironment.getParserEnv(parsoidConfig, envOptions); |
| 444 |
|
| 445 |
// Step 1: Fetch page from PHP API |
| 446 |
const phpDoc = yield phpFetch(env, title, options.oldid); |
| 447 |
// Step 2: Fetch page from Parsoid API |
| 448 |
const parsoidDoc = yield parsoidFetch(env, title, { |
| 449 |
domain, |
| 450 |
uri: options.parsoidURL, |
| 451 |
oldid: options.oldid || phpDoc.revid, |
| 452 |
useServer: options.useServer, |
| 453 |
}); |
| 454 |
// Step 3: Strip most markup (so we're comparing text, not markup) |
| 455 |
// ...but eventually we'll leave <a href> since there's some title |
| 456 |
// conversion that should be done. |
| 457 |
const normalize = out => `TITLE: ${ out.displaytitle }\n\n` + |
| 458 |
extractText(env, out.document); |
| 459 |
const phpText = normalize(phpDoc); |
| 460 |
const parsoidText = normalize(parsoidDoc); |
| 461 |
// Step 4: Compare (and profit!) |
| 462 |
console.assert(+phpDoc.revid === +parsoidDoc.revid); |
| 463 |
const output = formatter(null, domain, title, lang, options, { |
| 464 |
php: phpText, |
| 465 |
parsoid: parsoidText, |
| 466 |
revid: phpDoc.revid, |
| 467 |
}); |
| 468 |
const exitCode = (phpText === parsoidText) ? 0 : 1; |
| 469 |
|
| 470 |
return { |
| 471 |
output, |
| 472 |
exitCode, |
| 473 |
// List of local titles, in case we are spidering test cases |
| 474 |
linkedTitles: spiderDocument(env, parsoidDoc.document), |
| 475 |
}; |
| 476 |
})); |
| 477 |
|
| 478 |
if (require.main === module) { |
| 479 |
const standardOpts = ScriptUtils.addStandardOptions({ |
| 480 |
sourceVariant: { |
| 481 |
description: 'Force conversion to assume the given variant for' + |
| 482 |
' the source wikitext', |
| 483 |
boolean: false, |
| 484 |
default: null, |
| 485 |
}, |
| 486 |
domain: { |
| 487 |
description: 'Which wiki to use; e.g. "sr.wikipedia.org" for' + |
| 488 |
' Serbian wikipedia', |
| 489 |
boolean: false, |
| 490 |
default: 'sr.wikipedia.org', |
| 491 |
}, |
| 492 |
oldid: { |
| 493 |
description: 'Optional oldid of the given page. If not given,' + |
| 494 |
' will use the latest revision.', |
| 495 |
boolean: false, |
| 496 |
default: null, |
| 497 |
}, |
| 498 |
parsoidURL: { |
| 499 |
description: 'The URL for the Parsoid API', |
| 500 |
boolean: false, |
| 501 |
default: '', |
| 502 |
}, |
| 503 |
apiURL: { |
| 504 |
description: 'http path to remote API,' + |
| 505 |
' e.g. http://sr.wikipedia.org/w/api.php', |
| 506 |
boolean: false, |
| 507 |
default: '', |
| 508 |
}, |
| 509 |
xml: { |
| 510 |
description: 'Use xml output format', |
| 511 |
boolean: true, |
| 512 |
default: false, |
| 513 |
}, |
| 514 |
check: { |
| 515 |
description: 'Exit with non-zero exit code if differences found using selser', |
| 516 |
boolean: true, |
| 517 |
default: false, |
| 518 |
alias: 'c', |
| 519 |
}, |
| 520 |
'record': { |
| 521 |
description: 'Record http requests for later replay', |
| 522 |
'boolean': true, |
| 523 |
'default': false, |
| 524 |
}, |
| 525 |
'replay': { |
| 526 |
description: 'Replay recorded http requests for later replay', |
| 527 |
'boolean': true, |
| 528 |
'default': false, |
| 529 |
}, |
| 530 |
'spider': { |
| 531 |
description: 'Spider <number> additional pages past the given one', |
| 532 |
'boolean': false, |
| 533 |
'default': 0, |
| 534 |
}, |
| 535 |
'silent': { |
| 536 |
description: 'Skip output (used with --record --spider to load caches)', |
| 537 |
'boolean': true, |
| 538 |
'default': false, |
| 539 |
}, |
| 540 |
'verbose': { |
| 541 |
description: 'Log at level "info" as well', |
| 542 |
'boolean': true, |
| 543 |
'default': false, |
| 544 |
}, |
| 545 |
'useServer': { |
| 546 |
description: 'Use a parsoid server', |
| 547 |
'boolean': true, |
| 548 |
'default': false, |
| 549 |
}, |
| 550 |
}); |
| 551 |
|
| 552 |
Promise.async(function *() { |
| 553 |
const opts = yargs |
| 554 |
.usage( |
| 555 |
'Usage: $0 [options] <page-title> <variantLanguage>\n' + |
| 556 |
'The page title should be the "true title",' + |
| 557 |
'i.e., without any url encoding which might be necessary if it appeared in wikitext.' + |
| 558 |
'\n\n' |
| 559 |
) |
| 560 |
.options(standardOpts) |
| 561 |
.strict(); |
| 562 |
const argv = opts.argv; |
| 563 |
if (!argv._.length) { |
| 564 |
return opts.showHelp(); |
| 565 |
} |
| 566 |
const title = String(argv._[0]); |
| 567 |
const lang = String(argv._[1]); |
| 568 |
if (argv.record || argv.replay) { |
| 569 |
// Don't fork a separate server if record/replay |
| 570 |
argv.useServer = false; |
| 571 |
} |
| 572 |
if (argv.useServer && !argv.parsoidURL) { |
| 573 |
throw new Error('No parsoidURL provided!'); |
| 574 |
} |
| 575 |
const formatter = |
| 576 |
ScriptUtils.booleanOption(argv.silent) ? silentFormat : |
| 577 |
ScriptUtils.booleanOption(argv.xml) ? xmlFormat : |
| 578 |
plainFormat; |
| 579 |
const domain = argv.domain || 'sr.wikipedia.org'; |
| 580 |
const queue = [title]; |
| 581 |
const titlesDone = new Set(); |
| 582 |
let exitCode = 0; |
| 583 |
let r; |
| 584 |
for (let i = 0; i < queue.length; i++) { |
| 585 |
if (titlesDone.has(queue[i])) { |
| 586 |
continue; // duplicate title |
| 587 |
} |
| 588 |
if (argv.spider > 1 && argv.verbose) { |
| 589 |
console.log('%s (%d/%d)', queue[i], titlesDone.size, argv.spider); |
| 590 |
} |
| 591 |
try { |
| 592 |
r = yield runTest(domain, queue[i], lang, argv, formatter); |
| 593 |
} catch (e) { |
| 594 |
if (e instanceof DoesNotExistError && argv.spider > 1) { |
| 595 |
// Ignore page-not-found if we are spidering. |
| 596 |
continue; |
| 597 |
} |
| 598 |
r = { |
| 599 |
error: true, |
| 600 |
output: formatter(e, domain, queue[i], lang, argv), |
| 601 |
exitCode: 2, |
| 602 |
}; |
| 603 |
} |
| 604 |
exitCode = Math.max(exitCode, r.exitCode); |
| 605 |
if (r.output) { |
| 606 |
console.log(r.output); |
| 607 |
} |
| 608 |
// optionally, spider |
| 609 |
if (argv.spider > 1) { |
| 610 |
if (!r.error) { |
| 611 |
titlesDone.add(queue[i]); |
| 612 |
for (const t of r.linkedTitles) { |
| 613 |
if (/:/.test(t)) { |
| 614 |
continue; /* hack: no namespaces */ |
| 615 |
} |
| 616 |
queue.push(t); |
| 617 |
} |
| 618 |
} |
| 619 |
if (titlesDone.size >= argv.spider) { |
| 620 |
break; /* done! */ |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
if (argv.check || exitCode > 1) { |
| 625 |
process.exit(exitCode); |
| 626 |
} |
| 627 |
})().done(); |
| 628 |
} else if (typeof module === 'object') { |
| 629 |
module.exports.runTest = runTest; |
| 630 |
|
| 631 |
module.exports.jsonFormat = jsonFormat; |
| 632 |
module.exports.plainFormat = plainFormat; |
| 633 |
module.exports.xmlFormat = xmlFormat; |
| 634 |
} |