Skip to content

Commit

Permalink
Merge b5aa8f4 into 42fa905
Browse files Browse the repository at this point in the history
  • Loading branch information
Pchelolo authored May 16, 2019
2 parents 42fa905 + b5aa8f4 commit 5c7176f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
63 changes: 35 additions & 28 deletions sys/parsoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,36 +584,37 @@ class ParsoidService {
}

let contentPromise;
if (etag && etag.suffix === 'stash' && from === 'html' && to === 'wikitext') {
contentPromise = this._getStashedContent(hyper, rp.domain, rp.title, rp.revision, tid);
if (from === 'wikitext') {
// For transforming from wikitext Parsoid currently doesn't use the original
// content. It could be used for optimizing the template expansions. See T98995
// Note: when resurrecting sending the original content to Parsoid we should
// account for the possibility that it's not in storage, so contentPromise might
// reject with 404. In this case we would just not provide it.
contentPromise = P.resolve(undefined);
} else {
contentPromise = this._getOriginalContent(hyper, req, rp.revision, tid);
}
return contentPromise.then((original) => {
// Check if parsoid metadata is present as it's required by parsoid.
if (!original['data-parsoid'].body ||
if (etag && etag.suffix === 'stash' && from === 'html' && to === 'wikitext') {
contentPromise = this._getStashedContent(hyper, rp.domain,
rp.title, rp.revision, tid);
} else {
contentPromise = this._getOriginalContent(hyper, req, rp.revision, tid);
}
contentPromise = contentPromise
.tap((original) => {
// Check if parsoid metadata is present as it's required by parsoid.
if (!original['data-parsoid'].body ||
original['data-parsoid'].body.constructor !== Object ||
!original['data-parsoid'].body.ids) {
throw new HTTPError({
status: 400,
body: {
type: 'bad_request',
description: 'The page/revision has no associated Parsoid data'
}
});
}

const body2 = {
original,
[from]: req.body[from],
scrub_wikitext: req.body.scrub_wikitext,
body_only: req.body.body_only
};
// Let the stash flag through as well
if (req.body.stash) {
body2.stash = true;
}

throw new HTTPError({
status: 400,
body: {
type: 'bad_request',
description: 'The page/revision has no associated Parsoid data'
}
});
}
});
}
return contentPromise.then((original) => {
const path = [rp.domain, 'sys', 'parsoid', 'transform', from, 'to', to];
if (rp.title) {
path.push(rp.title);
Expand All @@ -628,7 +629,13 @@ class ParsoidService {
'content-type': 'application/json',
'user-agent': req['user-agent']
},
body: body2
body: {
original,
[from]: req.body[from],
scrub_wikitext: req.body.scrub_wikitext,
body_only: req.body.body_only,
stash: req.body.stash
}
};
return this.callParsoidTransform(hyper, newReq, from, to);
});
Expand Down
27 changes: 21 additions & 6 deletions test/features/parsoid/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const assert = require('../../utils/assert.js');
const Server = require('../../utils/server.js');
const preq = require('preq');
const parallel = require('mocha.parallel');

const testPage = {
title: 'User:Pchelolo%2fRestbase_Test',
Expand Down Expand Up @@ -32,7 +31,7 @@ describe('transform api', function() {

it('wt2html', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/User:GWicke%2F_restbase_test`,
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`,
body: {
wikitext: '== Heading =='
}
Expand All @@ -42,15 +41,31 @@ describe('transform api', function() {
assert.contentType(res, contentTypes.html);
const pattern = /<h2.*>Heading<\/h2>/;
if (!pattern.test(res.body)) {
throw new Error(`Expected pattern in response: ${pattern
}\nSaw: ${res.body}`);
throw new Error(`Expected pattern in response: ${pattern}\nSaw: ${res.body}`);
}
});
});

it('wt2html, title-recision for a new page', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/User:Pchelolo%2FRESTBaseTestPage_transform/393301`,
body: {
wikitext: '== Heading =='
}
})
.then((res) => {
assert.deepEqual(res.status, 200);
assert.contentType(res, contentTypes.html);
const pattern = /<h2.*>Heading<\/h2>/;
if (!pattern.test(res.body)) {
throw new Error(`Expected pattern in response: ${pattern}\nSaw: ${res.body}`);
}
});
});

it('wt2html with body_only', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/User:GWicke%2F_restbase_test`,
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`,
body: {
wikitext: '== Heading ==',
body_only: true
Expand Down Expand Up @@ -94,7 +109,7 @@ describe('transform api', function() {

it('html2wt, no-selser', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/User:GWicke%2F_restbase_test`,
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}`,
body: {
html: '<body>The modified HTML</body>'
}
Expand Down

0 comments on commit 5c7176f

Please sign in to comment.