Change ArticleTargetLoader#requestPageData API to use options object

Change-Id: I02e6218a8359b1cbc8fed1a34d68206a2a154d46
This commit is contained in:
Ed Sanders 2017-09-11 15:53:50 +01:00
parent 508fdf1543
commit 682787f312
4 changed files with 61 additions and 70 deletions

View file

@ -246,16 +246,14 @@
// requested already but it might not have finished loading yet
dataPromise = mw.loader.using( 'ext.visualEditor.targetLoader' )
.then( function () {
return mw.libs.ve.targetLoader.requestPageData(
mode,
mw.config.get( 'wgRelevantPageName' ),
section,
oldid,
'mwTarget', // ve.init.mw.DesktopArticleTarget.static.name
modified,
return mw.libs.ve.targetLoader.requestPageData( mode, mw.config.get( 'wgRelevantPageName' ), {
section: section,
oldId: oldid,
targetName: 'article', // ve.init.mw.DesktopArticleTarget.static.name
modified: modified,
// If switching to visual, check if we have wikitext to convert
mode === 'visual' ? $( '#wpTextbox1' ).textSelection( 'getContents' ) : undefined
);
wikitext: mode === 'visual' ? $( '#wpTextbox1' ).textSelection( 'getContents' ) : undefined
} );
} )
.done( incrementLoadingProgress );
}

View file

@ -1051,11 +1051,10 @@ ve.init.mw.ArticleTarget.prototype.getVisualDiffGeneratorPromise = function () {
if ( !this.fromEditedState ) {
this.originalDmDoc = this.constructor.static.createModelFromDom( this.doc, 'visual' );
} else {
mw.libs.ve.targetLoader.requestParsoidData(
this.pageName,
this.revid,
this.constructor.name
).then( function ( response ) {
mw.libs.ve.targetLoader.requestParsoidData( this.pageName, {
oldId: this.revid,
targetName: 'diff'
} ).then( function ( response ) {
var doc, data = response ? ( response.visualeditor || response.visualeditoredit ) : null;
if ( data && typeof data.content === 'string' ) {
doc = target.constructor.static.parseDocument( data.content, 'visual' );
@ -1145,13 +1144,11 @@ ve.init.mw.ArticleTarget.prototype.load = function ( dataPromise ) {
this.events.trackActivationStart( mw.libs.ve.activationStart );
mw.libs.ve.activationStart = null;
this.loading = dataPromise || mw.libs.ve.targetLoader.requestPageData(
this.getDefaultMode(),
this.pageName,
this.section,
this.requestedRevId,
this.constructor.name
);
this.loading = dataPromise || mw.libs.ve.targetLoader.requestPageData( this.getDefaultMode(), this.pageName, {
section: this.section,
oldId: this.requestedRevId,
targetName: this.constructor.static.name
} );
this.loading
.done( this.loadSuccess.bind( this ) )
.fail( this.loadFail.bind( this ) );
@ -2173,13 +2170,11 @@ ve.init.mw.ArticleTarget.prototype.switchToWikitextEditor = function ( discardCh
if ( ve.init.target.isModeAvailable( 'source' ) && !leaveVE ) {
if ( discardChanges ) {
dataPromise = mw.libs.ve.targetLoader.requestPageData(
'source',
this.pageName,
this.section,
this.requestedRevId,
this.constructor.name
).then(
dataPromise = mw.libs.ve.targetLoader.requestPageData( 'source', this.pageName, {
section: this.section,
oldId: this.requestedRevId,
targetName: this.constructor.static.name
} ).then(
function ( response ) { return response; },
function () {
// TODO: Some sort of progress bar?
@ -2246,13 +2241,12 @@ ve.init.mw.ArticleTarget.prototype.switchToVisualEditor = function () {
windowManager.destroy();
} );
} else {
dataPromise = mw.libs.ve.targetLoader.requestParsoidData(
this.pageName,
this.revid,
this.constructor.name,
this.edited,
this.getDocToSave()
);
dataPromise = mw.libs.ve.targetLoader.requestParsoidData( this.pageName, {
oldId: this.revid,
targetName: this.constructor.static.name,
modified: this.edited,
wikitext: this.getDocToSave()
} );
this.reloadSurface( 'visual', dataPromise );
}

View file

@ -99,18 +99,19 @@
*
* @param {string} mode Target mode: 'visual' or 'source'
* @param {string} pageName Page name to request
* @param {number|string} [section] Section to edit, number or 'new' (currently just source mode)
* @param {string} [oldid] Old revision ID, current if omitted
* @param {string} [targetName] Optional target name for tracking
* @param {boolean} [modified] The page was been modified before loading (e.g. in source mode)
* @param {string} [wikitext] Wikitext to convert to HTML. The original document is fetched if undefined.
* @param {Object} [options] Options
* @param {number|string} [options.section] Section to edit, number or 'new' (currently just source mode)
* @param {number} [options.oldId] Old revision ID. Current if omitted.
* @param {string} [options.targetName] Optional target name for tracking
* @param {boolean} [options.modified] The page was been modified before loading (e.g. in source mode)
* @param {string} [options.wikitext] Wikitext to convert to HTML. The original document is fetched if undefined.
* @return {jQuery.Promise} Abortable promise resolved with a JSON object
*/
requestPageData: function ( mode, pageName, section, oldid, targetName, modified, wikitext ) {
requestPageData: function ( mode, pageName, options ) {
if ( mode === 'source' ) {
return this.requestWikitext( pageName, section, oldid, targetName, modified );
return this.requestWikitext( pageName, options );
} else {
return this.requestParsoidData( pageName, oldid, targetName, modified, wikitext );
return this.requestParsoidData( pageName, options );
}
},
@ -118,14 +119,11 @@
* Request the page HTML and various metadata from the MediaWiki API (which will use
* Parsoid or RESTBase).
*
* @param {string} pageName Page name to request
* @param {string} [oldid] Old revision ID, current if omitted
* @param {string} [targetName] Optional target name for tracking
* @param {boolean} [modified] The page was been modified before loading (e.g. in source mode)
* @param {string} [wikitext] Wikitext to convert to HTML. The original document is fetched if undefined.
* @param {string} pageName See #requestPageData
* @param {Object} [options] See #requestPageData
* @return {jQuery.Promise} Abortable promise resolved with a JSON object
*/
requestParsoidData: function ( pageName, oldid, targetName, modified, wikitext ) {
requestParsoidData: function ( pageName, options ) {
var start, apiXhr, restbaseXhr, apiPromise, restbasePromise, dataPromise, pageHtmlUrl, headers,
switched = false,
fromEditedState = false,
@ -137,12 +135,14 @@
editintro: uri.query.editintro
};
options = options || {};
// Only request the API to explicitly load the currently visible revision if we're restoring
// from oldid. Otherwise we should load the latest version. This prevents us from editing an
// old version if an edit was made while the user was viewing the page and/or the user is
// seeing (slightly) stale cache.
if ( oldid !== undefined ) {
data.oldid = oldid;
if ( options.oldId !== undefined ) {
data.oldid = options.oldId;
}
// Load DOM
start = ve.now();
@ -155,7 +155,7 @@
bytes: $.byteLength( jqxhr.responseText ),
duration: ve.now() - start,
cacheHit: /hit/i.test( jqxhr.getResponseHeader( 'X-Cache' ) ),
targetName: targetName
targetName: options.targetName
} );
return data;
} );
@ -169,9 +169,10 @@
'Api-User-Agent': 'VisualEditor-MediaWiki/' + mw.config.get( 'wgVersion' )
};
// Convert specified Wikitext to HTML
if (
// wikitext can be an empty string
wikitext !== undefined &&
options.wikitext !== undefined &&
!$( '[name=wpSection]' ).val()
) {
if ( conf.fullRestbaseUrl ) {
@ -180,23 +181,24 @@
pageHtmlUrl = conf.restbaseUrl.replace( 'v1/page/html/', 'v1/transform/wikitext/to/html/' );
}
switched = true;
fromEditedState = modified;
fromEditedState = options.modified;
window.onbeforeunload = null;
$( window ).off( 'beforeunload' );
restbaseXhr = $.ajax( {
url: pageHtmlUrl + encodeURIComponent( pageName ) +
( oldid === undefined ? '' : '/' + oldid ),
( data.oldid === undefined ? '' : '/' + data.oldid ),
type: 'POST',
data: {
title: pageName,
oldid: oldid,
wikitext: wikitext,
oldid: data.oldid,
wikitext: options.wikitext,
stash: 'true'
},
headers: headers,
dataType: 'text'
} );
} else {
// Fetch revision
if ( conf.fullRestbaseUrl ) {
pageHtmlUrl = conf.fullRestbaseUrl + 'v1/page/html/';
} else {
@ -204,7 +206,7 @@
}
restbaseXhr = $.ajax( {
url: pageHtmlUrl + encodeURIComponent( pageName ) +
( oldid === undefined ? '' : '/' + oldid ) + '?redirect=false',
( data.oldid === undefined ? '' : '/' + data.oldid ) + '?redirect=false',
type: 'GET',
headers: headers,
dataType: 'text'
@ -216,7 +218,7 @@
ve.track( 'mwtiming.performance.system.restbaseLoad', {
bytes: $.byteLength( jqxhr.responseText ),
duration: ve.now() - start,
targetName: targetName
targetName: options.targetName
} );
return [ data, jqxhr.getResponseHeader( 'etag' ) ];
},
@ -255,14 +257,11 @@
/**
* Request the page wikitext and various metadata from the MediaWiki API.
*
* @param {string} pageName Page name to request
* @param {number|string} [section] Section to edit, number or 'new' (currently just source mode)
* @param {string} [oldid] Old revision ID, current if omitted
* @param {string} [targetName] Optional target name for tracking
* @param {boolean} [modified] The page was been modified before loading
* @param {string} pageName See #requestPageData
* @param {Object} [options] See #requestPageData
* @return {jQuery.Promise} Abortable promise resolved with a JSON object
*/
requestWikitext: function ( pageName, section, oldid ) {
requestWikitext: function ( pageName, options ) {
var data = {
action: 'visualeditor',
paction: 'wikitext',
@ -272,12 +271,12 @@
};
// section should never really be undefined, but check just in case
if ( section !== null && section !== undefined ) {
data.section = section;
if ( options.section !== null && options.section !== undefined ) {
data.section = options.section;
}
if ( oldid !== undefined ) {
data.oldid = oldid;
if ( options.oldId !== undefined ) {
data.oldid = options.oldId;
}
return new mw.Api().get( data );

View file

@ -50,7 +50,7 @@
revCache[ pageName ] = revCache[ pageName ] || {};
revCache[ pageName ][ revId ] =
revCache[ pageName ][ revId ] ||
mw.libs.ve.targetLoader.requestParsoidData( pageName, revId, 'diff' ).then( function ( response ) {
mw.libs.ve.targetLoader.requestParsoidData( pageName, { oldId: revId, targetName: 'diff' } ).then( function ( response ) {
return parseDocumentModulePromise.then( function () {
return mw.libs.ve.diffLoader.getModelFromResponse( response );
} );