2019-11-01 15:45:27 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki ArticleTargetSaver.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2019-11-01 15:45:27 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Target saver.
|
|
|
|
*
|
|
|
|
* Light-weight saver.
|
|
|
|
*
|
|
|
|
* @class mw.libs.ve.targetSaver
|
|
|
|
* @singleton
|
2024-04-30 11:02:45 +00:00
|
|
|
* @hideconstructor
|
2019-11-01 15:45:27 +00:00
|
|
|
*/
|
|
|
|
( function () {
|
|
|
|
mw.libs.ve = mw.libs.ve || {};
|
|
|
|
|
|
|
|
mw.libs.ve.targetSaver = {
|
|
|
|
/**
|
|
|
|
* Preload the library required for deflating so the user doesn't
|
|
|
|
* have to wait when postHtml is called.
|
|
|
|
*/
|
|
|
|
preloadDeflate: function () {
|
|
|
|
mw.loader.load( 'mediawiki.deflate' );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compress a string with deflate.
|
|
|
|
*
|
|
|
|
* @param {string} html HTML to deflate
|
|
|
|
* @return {jQuery.Promise} Promise resolved with deflated HTML
|
|
|
|
*/
|
|
|
|
deflate: function ( html ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
return mw.loader.using( 'mediawiki.deflate' ).then( () => mw.deflate( html ) );
|
2019-11-01 15:45:27 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get HTML to send to Parsoid.
|
|
|
|
*
|
|
|
|
* If the document was generated from scratch (e.g. inside VisualEditor's converter), the
|
|
|
|
* source document can be passed in to transplant the head tag, as well as the attributes
|
|
|
|
* on the html and body tags.
|
|
|
|
*
|
|
|
|
* @param {HTMLDocument} newDoc Document generated by ve.dm.Converter. Will be modified.
|
|
|
|
* @param {HTMLDocument} [oldDoc] Old document to copy attributes from.
|
|
|
|
* @return {string} Full HTML document
|
|
|
|
*/
|
|
|
|
getHtml: function ( newDoc, oldDoc ) {
|
|
|
|
function copyAttributes( from, to ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( from.attributes, ( attr ) => {
|
2020-08-18 22:17:03 +00:00
|
|
|
to.setAttribute( attr.name, attr.value );
|
|
|
|
} );
|
2019-11-01 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( oldDoc ) {
|
|
|
|
// Copy the head from the old document
|
2021-05-05 15:56:49 +00:00
|
|
|
for ( var i = 0, len = oldDoc.head.childNodes.length; i < len; i++ ) {
|
2019-11-01 15:45:27 +00:00
|
|
|
newDoc.head.appendChild( oldDoc.head.childNodes[ i ].cloneNode( true ) );
|
|
|
|
}
|
|
|
|
// Copy attributes from the old document for the html, head and body
|
|
|
|
copyAttributes( oldDoc.documentElement, newDoc.documentElement );
|
|
|
|
copyAttributes( oldDoc.head, newDoc.head );
|
|
|
|
copyAttributes( oldDoc.body, newDoc.body );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out junk that may have been added by browser plugins
|
|
|
|
$( newDoc )
|
|
|
|
.find( [
|
|
|
|
'script', // T54884, T65229, T96533, T103430
|
|
|
|
'noscript', // T144891
|
|
|
|
'object', // T65229
|
2024-04-17 13:19:12 +00:00
|
|
|
'style:not( [ data-mw ] ):not( [ data-mw-deduplicate ] )', // T55252, but allow <style data-mw(-deduplicate)/> e.g. TemplateStyles T188143
|
2019-11-01 15:45:27 +00:00
|
|
|
'embed', // T53521, T54791, T65121
|
|
|
|
'a[href^="javascript:"]', // T200971
|
|
|
|
'img[src^="data:"]', // T192392
|
|
|
|
'div[id="myEventWatcherDiv"]', // T53423
|
|
|
|
'div[id="sendToInstapaperResults"]', // T63776
|
|
|
|
'div[id="kloutify"]', // T69006
|
|
|
|
'div[id^="mittoHidden"]', // T70900
|
|
|
|
'div.hon.certificateLink', // HON (T209619)
|
2021-12-20 17:54:20 +00:00
|
|
|
'div.donut-container', // Web of Trust (T189148)
|
|
|
|
'div.shield-container' // Web of Trust (T297862)
|
2019-11-01 15:45:27 +00:00
|
|
|
].join( ',' ) )
|
2021-12-21 13:19:37 +00:00
|
|
|
.each( function () {
|
|
|
|
function truncate( text, l ) {
|
|
|
|
return text.length > l ? text.slice( 0, l ) + '…' : text;
|
|
|
|
}
|
|
|
|
var errorMessage = 'DOM content matching deny list found:\n' + truncate( this.outerHTML, 100 ) +
|
|
|
|
'\nContext:\n' + truncate( this.parentNode.outerHTML, 200 );
|
|
|
|
mw.log.error( errorMessage );
|
|
|
|
var err = new Error( errorMessage );
|
|
|
|
err.name = 'VeDomDenyListWarning';
|
|
|
|
mw.errorLogger.logError( err, 'error.visualeditor' );
|
|
|
|
$( this ).remove();
|
|
|
|
} );
|
2019-11-01 15:45:27 +00:00
|
|
|
|
2020-03-20 17:08:24 +00:00
|
|
|
// data-mw-section-id is copied to headings by mw.libs.ve.unwrapParsoidSections
|
2019-11-01 15:45:27 +00:00
|
|
|
// Remove these to avoid triggering selser.
|
|
|
|
$( newDoc ).find( '[data-mw-section-id]:not( section )' ).removeAttr( 'data-mw-section-id' );
|
|
|
|
|
2022-01-24 21:11:29 +00:00
|
|
|
// Deduplicate styles (we re-duplicated them in ve.init.mw.Target.static.parseDocument)
|
|
|
|
// to let selser recognize the nodes and avoid dirty diffs.
|
|
|
|
mw.libs.ve.deduplicateStyles( newDoc.body );
|
|
|
|
|
2019-11-01 15:45:27 +00:00
|
|
|
// Add doctype manually
|
2023-04-12 15:32:59 +00:00
|
|
|
// ve.properOuterHtml is loaded separately in ve.utils.parsing.js
|
2019-11-01 15:45:27 +00:00
|
|
|
// eslint-disable-next-line no-undef
|
2023-04-12 15:32:59 +00:00
|
|
|
return '<!doctype html>' + ve.properOuterHtml( newDoc.documentElement );
|
2019-11-01 15:45:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize and deflate an HTML document
|
|
|
|
*
|
|
|
|
* @param {HTMLDocument} doc Document generated by ve.dm.Converter. Will be modified.
|
|
|
|
* @param {HTMLDocument} [oldDoc] Old document to copy attributes from.
|
|
|
|
* @return {jQuery.Promise} Promise resolved with deflated HTML
|
|
|
|
*/
|
|
|
|
deflateDoc: function ( doc, oldDoc ) {
|
|
|
|
return this.deflate( this.getHtml( doc, oldDoc ) );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post an HTML document to the API.
|
|
|
|
*
|
|
|
|
* Serializes the document to HTML, deflates it, then passes to #postHtml.
|
|
|
|
*
|
|
|
|
* @param {HTMLDocument} doc Document to save
|
2020-01-24 23:32:46 +00:00
|
|
|
* @param {Object} [extraData] Extra data to send to the API
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {Object} [options]
|
2019-11-01 15:45:27 +00:00
|
|
|
* @return {jQuery.Promise} Promise which resolves if the post was successful
|
|
|
|
*/
|
|
|
|
saveDoc: function ( doc, extraData, options ) {
|
2020-01-24 23:32:46 +00:00
|
|
|
var saver = this;
|
2024-04-30 16:44:25 +00:00
|
|
|
return this.deflateDoc( doc ).then( ( html ) => saver.postHtml(
|
|
|
|
html,
|
|
|
|
null,
|
|
|
|
extraData,
|
|
|
|
options
|
|
|
|
) );
|
2019-11-01 15:45:27 +00:00
|
|
|
},
|
|
|
|
|
2019-11-04 14:54:53 +00:00
|
|
|
/**
|
|
|
|
* Post wikitext to the API.
|
|
|
|
*
|
|
|
|
* By default uses action=visualeditoredit, paction=save.
|
|
|
|
*
|
|
|
|
* @param {string} wikitext Wikitext to post. Deflating is optional but recommended.
|
|
|
|
* @param {Object} [extraData] Extra data to send to the API
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {Object} [options]
|
2019-11-04 14:54:53 +00:00
|
|
|
* @param {mw.Api} [options.api] Api to use
|
|
|
|
* @param {Function} [options.now] Function returning current time in milliseconds for tracking, e.g. ve.now
|
|
|
|
* @param {Function} [options.track] Tracking function
|
|
|
|
* @param {string} [options.eventName] Event name for tracking
|
|
|
|
* @return {jQuery.Promise} Promise which resolves with API save data, or rejects with error details
|
|
|
|
*/
|
|
|
|
postWikitext: function ( wikitext, extraData, options ) {
|
|
|
|
return this.postContent( $.extend( { wikitext: wikitext }, extraData ), options );
|
|
|
|
},
|
|
|
|
|
2019-11-01 15:45:27 +00:00
|
|
|
/**
|
|
|
|
* Post HTML to the API.
|
|
|
|
*
|
|
|
|
* By default uses action=visualeditoredit, paction=save.
|
|
|
|
*
|
|
|
|
* @param {string} html HTML to post. Deflating is optional but recommended.
|
|
|
|
* Should be included for retries even if a cache key is provided.
|
|
|
|
* @param {string} [cacheKey] Optional cache key of HTML stashed on server.
|
|
|
|
* @param {Object} [extraData] Extra data to send to the API
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {Object} [options]
|
2019-11-04 14:54:53 +00:00
|
|
|
* @return {jQuery.Promise} Promise which resolves with API save data, or rejects with error details
|
|
|
|
*/
|
|
|
|
postHtml: function ( html, cacheKey, extraData, options ) {
|
2021-05-05 15:56:49 +00:00
|
|
|
var saver = this;
|
2019-11-04 14:54:53 +00:00
|
|
|
|
2019-12-11 04:47:47 +00:00
|
|
|
options = options || {};
|
2021-05-05 15:56:49 +00:00
|
|
|
var data;
|
2019-11-04 14:54:53 +00:00
|
|
|
if ( cacheKey ) {
|
|
|
|
data = $.extend( { cachekey: cacheKey }, extraData );
|
|
|
|
} else {
|
|
|
|
data = $.extend( { html: html }, extraData );
|
|
|
|
}
|
|
|
|
return this.postContent( data, options ).then(
|
|
|
|
null,
|
2024-04-30 16:44:25 +00:00
|
|
|
( code, response ) => {
|
2019-11-04 14:54:53 +00:00
|
|
|
// This cache key is evidently bad, clear it
|
|
|
|
if ( options.onCacheKeyFail ) {
|
|
|
|
options.onCacheKeyFail();
|
|
|
|
}
|
|
|
|
if ( code === 'badcachekey' ) {
|
|
|
|
// If the cache key failed, try again without the cache key
|
|
|
|
return saver.postHtml(
|
|
|
|
html,
|
|
|
|
null,
|
|
|
|
extraData,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Failed for some other reason - let caller handle it.
|
|
|
|
return $.Deferred().reject( code, response ).promise();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-02-15 02:50:16 +00:00
|
|
|
* Post content to the API, using mw.Api#postWithToken to retry automatically when encountering
|
|
|
|
* a 'badtoken' error.
|
2019-11-04 14:54:53 +00:00
|
|
|
*
|
|
|
|
* By default uses action=visualeditoredit, paction=save.
|
|
|
|
*
|
|
|
|
* @param {string} data Content data
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {Object} [options]
|
2019-11-01 15:45:27 +00:00
|
|
|
* @param {mw.Api} [options.api] Api to use
|
|
|
|
* @param {Function} [options.now] Function returning current time in milliseconds for tracking, e.g. ve.now
|
|
|
|
* @param {Function} [options.track] Tracking function
|
|
|
|
* @param {string} [options.eventName] Event name for tracking
|
2019-11-04 14:54:53 +00:00
|
|
|
* @return {jQuery.Promise} Promise which resolves with API save data, or rejects with error details
|
2019-11-01 15:45:27 +00:00
|
|
|
*/
|
2019-11-04 14:54:53 +00:00
|
|
|
postContent: function ( data, options ) {
|
2019-11-01 15:45:27 +00:00
|
|
|
options = options || {};
|
2021-05-05 15:56:49 +00:00
|
|
|
var api = options.api || new mw.Api();
|
2019-11-01 15:45:27 +00:00
|
|
|
|
2021-05-05 15:56:49 +00:00
|
|
|
var start;
|
2019-11-01 15:45:27 +00:00
|
|
|
if ( options.now ) {
|
|
|
|
start = options.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
data = $.extend(
|
|
|
|
{
|
|
|
|
action: 'visualeditoredit',
|
|
|
|
paction: 'save',
|
2022-02-18 15:52:47 +00:00
|
|
|
useskin: mw.config.get( 'skin' ),
|
|
|
|
// Same as OO.ui.isMobile()
|
|
|
|
mobileformat: !!mw.config.get( 'wgMFMode' ),
|
2019-11-04 14:54:53 +00:00
|
|
|
formatversion: 2,
|
|
|
|
errorformat: 'html',
|
|
|
|
errorlang: mw.config.get( 'wgUserLanguage' ),
|
2023-03-18 02:34:32 +00:00
|
|
|
errorsuselocal: true
|
2019-11-01 15:45:27 +00:00
|
|
|
},
|
|
|
|
data
|
|
|
|
);
|
|
|
|
|
2021-05-05 15:56:49 +00:00
|
|
|
var action = data.action;
|
2020-07-02 10:25:26 +00:00
|
|
|
|
2023-03-18 02:34:32 +00:00
|
|
|
var request = api.postWithToken( 'csrf', data, {
|
|
|
|
contentType: 'multipart/form-data',
|
|
|
|
trackEditAttemptStepSessionId: true
|
|
|
|
} );
|
2020-02-15 02:50:16 +00:00
|
|
|
|
2019-11-01 15:45:27 +00:00
|
|
|
return request.then(
|
2024-04-30 16:44:25 +00:00
|
|
|
( response, jqxhr ) => {
|
2021-05-05 15:56:49 +00:00
|
|
|
var responseData = response[ action ];
|
2019-11-04 14:54:53 +00:00
|
|
|
|
2019-11-01 15:45:27 +00:00
|
|
|
// Log data about the request if eventName was set
|
|
|
|
if ( options.track && options.eventName ) {
|
2021-05-05 15:56:49 +00:00
|
|
|
var eventData = {
|
2019-11-01 15:45:27 +00:00
|
|
|
bytes: require( 'mediawiki.String' ).byteLength( jqxhr.responseText ),
|
|
|
|
duration: options.now() - start
|
|
|
|
};
|
2021-05-05 15:56:49 +00:00
|
|
|
var fullEventName = 'performance.system.' + options.eventName +
|
2020-08-18 12:16:49 +00:00
|
|
|
( responseData.cachekey ? '.withCacheKey' : '.withoutCacheKey' );
|
2019-11-01 15:45:27 +00:00
|
|
|
options.track( fullEventName, eventData );
|
|
|
|
}
|
2019-11-04 14:54:53 +00:00
|
|
|
|
2021-05-05 15:56:49 +00:00
|
|
|
var error;
|
2020-08-18 12:16:49 +00:00
|
|
|
if ( !responseData ) {
|
2019-11-04 14:54:53 +00:00
|
|
|
error = {
|
|
|
|
code: 'invalidresponse',
|
2020-02-12 20:55:48 +00:00
|
|
|
html: mw.message( 'api-clientside-error-invalidresponse' ).parse()
|
2019-11-04 14:54:53 +00:00
|
|
|
};
|
2020-08-18 12:16:49 +00:00
|
|
|
} else if ( responseData.result !== 'success' ) {
|
2019-11-19 19:38:15 +00:00
|
|
|
// This should only happen when saving an edit and getting a captcha from ConfirmEdit
|
|
|
|
// extension (`data.result === 'error'`). It's a silly special case...
|
2019-11-18 21:05:33 +00:00
|
|
|
return $.Deferred().reject( 'no-error-no-success', response ).promise();
|
2019-11-04 14:54:53 +00:00
|
|
|
} else {
|
|
|
|
// paction specific errors
|
2020-08-18 12:16:49 +00:00
|
|
|
switch ( responseData.paction ) {
|
2019-11-04 14:54:53 +00:00
|
|
|
case 'save':
|
|
|
|
case 'serialize':
|
2020-08-18 12:16:49 +00:00
|
|
|
if ( typeof responseData.content !== 'string' ) {
|
2019-11-04 14:54:53 +00:00
|
|
|
error = {
|
|
|
|
code: 'invalidcontent',
|
2020-02-12 20:55:48 +00:00
|
|
|
html: mw.message( 'api-clientside-error-invalidresponse' ).parse()
|
2019-11-04 14:54:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'diff':
|
2020-08-18 12:16:49 +00:00
|
|
|
if ( typeof responseData.diff !== 'string' ) {
|
2019-11-04 14:54:53 +00:00
|
|
|
error = {
|
|
|
|
code: 'invalidcontent',
|
2020-02-12 20:55:48 +00:00
|
|
|
html: mw.message( 'api-clientside-error-invalidresponse' ).parse()
|
2019-11-04 14:54:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( error ) {
|
|
|
|
// Use the same format as API errors
|
|
|
|
return $.Deferred().reject( error.code, { errors: [ error ] } ).promise();
|
|
|
|
}
|
2020-08-18 12:16:49 +00:00
|
|
|
return responseData;
|
2019-11-01 15:45:27 +00:00
|
|
|
},
|
2024-04-30 16:44:25 +00:00
|
|
|
( code, response ) => {
|
2021-05-05 15:56:49 +00:00
|
|
|
var responseText = OO.getProp( response, 'xhr', 'responseText' );
|
2019-11-01 15:45:27 +00:00
|
|
|
|
|
|
|
if ( responseText && options.track && options.eventName ) {
|
2021-05-05 15:56:49 +00:00
|
|
|
var eventData = {
|
2019-11-01 15:45:27 +00:00
|
|
|
bytes: require( 'mediawiki.String' ).byteLength( responseText ),
|
|
|
|
duration: options.now() - start
|
|
|
|
};
|
2021-05-05 15:56:49 +00:00
|
|
|
var fullEventName;
|
2019-11-04 14:54:53 +00:00
|
|
|
if ( code === 'badcachekey' ) {
|
2019-11-01 15:45:27 +00:00
|
|
|
fullEventName = 'performance.system.' + options.eventName + '.badCacheKey';
|
|
|
|
} else {
|
|
|
|
fullEventName = 'performance.system.' + options.eventName + '.withoutCacheKey';
|
|
|
|
}
|
|
|
|
options.track( fullEventName, eventData );
|
|
|
|
}
|
2019-11-04 14:54:53 +00:00
|
|
|
return $.Deferred().reject( code, response ).promise();
|
2019-11-01 15:45:27 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}() );
|