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-11-21 13:23:31 +00:00
|
|
|
return mw.loader.using( 'mediawiki.deflate' ).then( () => mw.deflateAsync( 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
|
2024-05-21 14:22:56 +00:00
|
|
|
for ( let 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( ',' ) )
|
2024-05-01 11:36:18 +00:00
|
|
|
.each( ( j, el ) => {
|
2021-12-21 13:19:37 +00:00
|
|
|
function truncate( text, l ) {
|
|
|
|
return text.length > l ? text.slice( 0, l ) + '…' : text;
|
|
|
|
}
|
2024-05-21 14:22:56 +00:00
|
|
|
const errorMessage = 'DOM content matching deny list found:\n' + truncate( el.outerHTML, 100 ) +
|
2024-05-01 11:36:18 +00:00
|
|
|
'\nContext:\n' + truncate( el.parentNode.outerHTML, 200 );
|
2021-12-21 13:19:37 +00:00
|
|
|
mw.log.error( errorMessage );
|
2024-05-21 14:22:56 +00:00
|
|
|
const err = new Error( errorMessage );
|
2021-12-21 13:19:37 +00:00
|
|
|
err.name = 'VeDomDenyListWarning';
|
|
|
|
mw.errorLogger.logError( err, 'error.visualeditor' );
|
2024-05-01 11:36:18 +00:00
|
|
|
$( el ).remove();
|
2021-12-21 13:19:37 +00:00
|
|
|
} );
|
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 ) {
|
2024-05-01 12:32:49 +00:00
|
|
|
return this.deflateDoc( doc ).then( ( html ) => this.postHtml(
|
2024-04-30 16:44:25 +00:00
|
|
|
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 ) {
|
2019-12-11 04:47:47 +00:00
|
|
|
options = options || {};
|
2024-05-21 14:22:56 +00:00
|
|
|
let 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
|
2024-05-01 12:32:49 +00:00
|
|
|
return this.postHtml(
|
2019-11-04 14:54:53 +00:00
|
|
|
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 || {};
|
2024-05-21 14:22:56 +00:00
|
|
|
const api = options.api || new mw.Api();
|
2019-11-01 15:45:27 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let 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
|
|
|
|
);
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const action = data.action;
|
2020-07-02 10:25:26 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const request = api.postWithToken( 'csrf', data, {
|
2023-03-18 02:34:32 +00:00
|
|
|
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 ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const 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 ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const eventData = {
|
2019-11-01 15:45:27 +00:00
|
|
|
bytes: require( 'mediawiki.String' ).byteLength( jqxhr.responseText ),
|
|
|
|
duration: options.now() - start
|
|
|
|
};
|
2024-05-21 14:22:56 +00:00
|
|
|
const 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
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let 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 ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const responseText = OO.getProp( response, 'xhr', 'responseText' );
|
2019-11-01 15:45:27 +00:00
|
|
|
|
|
|
|
if ( responseText && options.track && options.eventName ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const eventData = {
|
2019-11-01 15:45:27 +00:00
|
|
|
bytes: require( 'mediawiki.String' ).byteLength( responseText ),
|
|
|
|
duration: options.now() - start
|
|
|
|
};
|
2024-05-21 14:22:56 +00:00
|
|
|
let 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
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}() );
|