Skip to content

Commit

Permalink
Prepare for MCR
Browse files Browse the repository at this point in the history
Send rvslots so that the mwapi stops warning about using the legacy
structure.

Also, removes the use of rvcontentformat that was added in e08a13b since
it's been deprecated.

See https://lists.wikimedia.org/pipermail/mediawiki-api-announce/2018-August/000140.html

Bug: T201115
Change-Id: Ic076c4deac896ffced5ba32a7cf3e9c6208d097e
  • Loading branch information
arlolra authored and jenkins-bot committed Aug 8, 2018
1 parent 889e1be commit 597932b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 47 deletions.
17 changes: 11 additions & 6 deletions lib/config/MWParserEnvironment.js
Expand Up @@ -404,9 +404,13 @@ MWParserEnvironment.prototype.cacheReusableExpansions = function(obj) {
* sha1:
* size: // in bytes
* comment:
* contentmodel:
* contentformat:
* "* ": // actual source text --> copied to this.page.src
* slots: {
* main: {
* contentmodel:
* contentformat:
* "* ": // actual source text --> copied to this.page.src
* }
* }
* }
* }
* ```
Expand All @@ -428,14 +432,15 @@ MWParserEnvironment.prototype.setPageSrcInfo = function(srcOrMetadata) {
m.title = metadata.title;
var r = m.revision;
if (!r) { r = m.revision = {}; }
var content = Util.getStar(metadata.revision);
if (metadata.revision) {
r.revid = metadata.revision.revid;
r.parentid = metadata.revision.parentid;
r.timestamp = metadata.revision.timestamp;
r.sha1 = metadata.revision.sha1;
r.size = metadata.revision.size;
r.contentmodel = metadata.revision.contentmodel;
r.contentformat = metadata.revision.contentformat;
r.contentmodel = content && content.contentmodel;
r.contentformat = content && content.contentformat;
}

// Update other page properties
Expand All @@ -444,7 +449,7 @@ MWParserEnvironment.prototype.setPageSrcInfo = function(srcOrMetadata) {
this.page.latest = metadata.latest;
this.page.pagelanguage = metadata.pagelanguage;
this.page.pagelanguagedir = metadata.pagelanguagedir;
this.page.src = (metadata.revision && metadata.revision['*']) || '';
this.page.src = (content && content['*']) || '';
this.page.setVariant(null);
};

Expand Down
21 changes: 8 additions & 13 deletions lib/mw/ApiRequest.js
Expand Up @@ -11,6 +11,7 @@ var url = require('url');
var util = require('util');

var Promise = require('../utils/promise.js');
var Util = require('../utils/Util.js').Util;


function setupConnectionTimeout(env, protocol) {
Expand Down Expand Up @@ -57,9 +58,6 @@ function setupConnectionTimeout(env, protocol) {

var latestSerial = 0;

// all revision properties which parsoid is interested in.
var PARSOID_RVPROP = ('content|ids|timestamp|size|sha1|contentmodel');

var logAPIWarnings = function(req, data) {
if (req.env.conf.parsoid.logMwApiWarnings &&
data && data.hasOwnProperty('warnings')) {
Expand Down Expand Up @@ -478,15 +476,11 @@ function TemplateRequest(env, title, oldid, opts) {
action: 'query',
prop: 'info|revisions',
rawcontinue: 1,
rvprop: PARSOID_RVPROP,
// all revision properties which parsoid is interested in.
rvprop: 'content|ids|timestamp|size|sha1|contentmodel',
rvslots: 'main',
};

// Allow request to give a specific contentformat
// (otherwise wikitext is the default)
if (opts.contentformat) {
apiargs.rvcontentformat = opts.contentformat;
}

if (oldid) {
this.oldid = oldid;
apiargs.revids = oldid;
Expand Down Expand Up @@ -533,7 +527,7 @@ TemplateRequest.prototype._handleJSON = function(error, data) {
return;
}

var metadata;
var metadata, content;
if (!data.query.pages) {
if (data.query.interwiki) {
// Essentially redirect, but don't actually redirect.
Expand Down Expand Up @@ -572,7 +566,8 @@ TemplateRequest.prototype._handleJSON = function(error, data) {
pagelanguage: page.pagelanguage,
pagelanguagedir: page.pagelanguagedir,
};
if (metadata.revision.texthidden || !metadata.revision.hasOwnProperty("*")) {
content = Util.getStar(metadata.revision);
if (metadata.revision.texthidden || !content || !content.hasOwnProperty('*')) {
error = new DoesNotExistError("Source is hidden for " + this.title);
}
return true;
Expand All @@ -590,7 +585,7 @@ TemplateRequest.prototype._handleJSON = function(error, data) {

// Add the source to the cache
// (both original title as well as possible redirected title)
this.env.pageCache[this.queueKey] = this.env.pageCache[this.title] = metadata.revision['*'];
this.env.pageCache[this.queueKey] = this.env.pageCache[this.title] = content['*'];

this._processListeners(null, metadata);
};
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/Util.js
Expand Up @@ -1653,6 +1653,14 @@ Util.validateMediaParam = function(num) {
return num > 0;
};

// Extract content in a backwards compatible way
Util.getStar = function(revision) {
var content = revision;
if (revision && revision.slots) {
content = revision.slots.main;
}
return content;
};

if (typeof module === "object") {
module.exports.Util = Util;
Expand Down
10 changes: 10 additions & 0 deletions tests/mocha/api.js
Expand Up @@ -567,6 +567,16 @@ describe('Parsoid API', function() {
.end(done);
});

it('should get from a title and revision (html, pre-mcr)', function(done) {
request(api)
.get(mockDomain + '/v3/page/html/Old_Response/999')
.expect(validHtmlResponse(function(doc) {
// SECTION -> P
doc.body.firstChild.firstChild.textContent.should.equal('MediaWiki was successfully installed.');
}))
.end(done);
});

it('should get from a title and revision (html, json content)', function(done) {
request(api)
.get(mockDomain + '/v3/page/html/JSON_Page/101')
Expand Down
120 changes: 92 additions & 28 deletions tests/mockAPI.js
Expand Up @@ -88,9 +88,35 @@ var mainPage = {
{
revid: 1,
parentid: 0,
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '<strong>MediaWiki has been successfully installed.</strong>\n\nConsult the [//meta.wikimedia.org/wiki/Help:Contents User\'s Guide] for information on using the wiki software.\n\n== Getting started ==\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]\n* [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language]',
},
},
},
],
},
},
},
};

// Old response structure, pre-mcr
var oldResponse = {
query: {
pages: {
'999': {
pageid: 999,
ns: 0,
title: 'Old Response',
revisions: [
{
revid: 999,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '<strong>MediaWiki has been successfully installed.</strong>\n\nConsult the [//meta.wikimedia.org/wiki/Help:Contents User\'s Guide] for information on using the wiki software.\n\n== Getting started ==\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]\n* [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language]',
'*': '<strong>MediaWiki was successfully installed.</strong>\n\nConsult the [//meta.wikimedia.org/wiki/Help:Contents User\'s Guide] for information on using the wiki software.\n\n== Getting started ==\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]\n* [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language]',
},
],
},
Expand All @@ -109,9 +135,13 @@ var junkPage = {
{
revid: 2,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '2. This is just some junk. See the comment above.',
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '2. This is just some junk. See the comment above.',
},
},
},
],
},
Expand All @@ -130,9 +160,13 @@ var largePage = {
{
revid: 3,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': 'a'.repeat(parsoidOptions.limits.wt2html.maxWikitextSize + 1),
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': 'a'.repeat(parsoidOptions.limits.wt2html.maxWikitextSize + 1),
},
},
},
],
},
Expand All @@ -151,9 +185,13 @@ var reusePage = {
{
revid: 100,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '{{colours of the rainbow}}',
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '{{colours of the rainbow}}',
},
},
},
],
},
Expand All @@ -172,9 +210,13 @@ var jsonPage = {
{
revid: 101,
parentid: 0,
contentmodel: 'json',
contentformat: 'text/json',
'*': '[1]',
slots: {
main: {
contentmodel: 'json',
contentformat: 'text/json',
'*': '[1]',
},
},
},
],
},
Expand All @@ -193,9 +235,13 @@ var lintPage = {
{
revid: 102,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '{|\nhi\n|ho\n|}',
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '{|\nhi\n|ho\n|}',
},
},
},
],
},
Expand All @@ -214,9 +260,13 @@ var redlinksPage = {
{
revid: 103,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '[[Special:Version]] [[Doesnotexist]] [[Redirected]]',
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '[[Special:Version]] [[Doesnotexist]] [[Redirected]]',
},
},
},
],
},
Expand All @@ -235,9 +285,13 @@ var variantPage = {
{
revid: 104,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': 'абвг abcd',
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': 'абвг abcd',
},
},
},
],
pagelanguage: 'sr',
Expand All @@ -258,9 +312,13 @@ var noVariantPage = {
{
revid: 105,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': 'абвг abcd\n__NOCONTENTCONVERT__',
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': 'абвг abcd\n__NOCONTENTCONVERT__',
},
},
},
],
pagelanguage: 'sr',
Expand All @@ -281,9 +339,13 @@ var revisionPage = {
{
revid: 63,
parentid: 0,
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '{{REVISIONID}}',
slots: {
main: {
contentmodel: 'wikitext',
contentformat: 'text/x-wiki',
'*': '{{REVISIONID}}',
},
},
},
],
},
Expand Down Expand Up @@ -547,6 +609,8 @@ var availableActions = {
return cb(null , variantPage);
} else if (body.revids === '105' || body.titles === 'No_Variant_Page') {
return cb(null , noVariantPage);
} else if (body.revids === '999' || body.titles === 'Old_Response') {
return cb(null , oldResponse);
} else {
return cb(null, { query: { pages: {
'-1': {
Expand Down

0 comments on commit 597932b

Please sign in to comment.