2010-09-15 22:40:50 +00:00
|
|
|
/*
|
|
|
|
* JavaScript for WikiEditor
|
|
|
|
*/
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2018-11-12 22:28:40 +00:00
|
|
|
( function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
var editingSessionId;
|
2019-06-27 21:48:25 +00:00
|
|
|
|
2019-07-20 08:09:04 +00:00
|
|
|
// This sets $.wikiEditor and $.fn.wikiEditor
|
|
|
|
require( './jquery.wikiEditor.js' );
|
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
function log() {
|
|
|
|
// mw.log is a no-op unless resource loader is in debug mode, so
|
|
|
|
// this allows trackdebug to work independently (T211698)
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log.apply( console, arguments );
|
|
|
|
}
|
|
|
|
|
2022-11-11 14:32:24 +00:00
|
|
|
function sampledLogger( schema, callback, transformCallback ) {
|
2021-06-15 14:45:13 +00:00
|
|
|
var trackdebug = !!mw.util.getParamValue( 'trackdebug' );
|
2019-06-27 21:48:25 +00:00
|
|
|
return function () {
|
|
|
|
if ( mw.loader.getState( 'ext.eventLogging' ) === null ) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-07 17:58:43 +00:00
|
|
|
if ( mw.config.get( 'wgMFMode' ) !== null ) {
|
|
|
|
// Visiting a ?action=edit URL can, depending on user settings, result
|
|
|
|
// in the MobileFrontend overlay appearing on top of WikiEditor. In
|
|
|
|
// these cases, don't log anything.
|
|
|
|
return;
|
|
|
|
}
|
2021-06-15 14:45:13 +00:00
|
|
|
var args = Array.prototype.slice.call( arguments );
|
2019-06-27 21:48:25 +00:00
|
|
|
|
|
|
|
mw.loader.using( [ 'ext.eventLogging' ] ).done( function () {
|
|
|
|
// Sampling
|
|
|
|
// We have to do this on the client too because the unload handler
|
|
|
|
// can cause an editingSessionId to be generated on the client
|
|
|
|
// Not using mw.eventLog.inSample() because we need to be able to pass our own editingSessionId
|
2021-06-15 14:45:13 +00:00
|
|
|
var inSample = mw.eventLog.randomTokenMatch(
|
|
|
|
1 / mw.config.get( 'wgWMESchemaEditAttemptStepSamplingRate' ),
|
|
|
|
editingSessionId
|
|
|
|
);
|
2019-06-27 21:48:25 +00:00
|
|
|
|
|
|
|
if ( !inSample && !mw.config.get( 'wgWMESchemaEditAttemptStepOversample' ) && !trackdebug ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var data = callback.apply( this, [ inSample ].concat( args ) );
|
2019-06-27 21:48:25 +00:00
|
|
|
|
|
|
|
if ( trackdebug ) {
|
|
|
|
log( schema, data );
|
2022-11-11 14:32:24 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mw.eventLog.logEvent( schema, data );
|
|
|
|
|
|
|
|
// T309013: If applicable, log via the Metrics Platform:
|
|
|
|
if ( transformCallback ) {
|
|
|
|
var transformed = transformCallback.apply( this, [ data ] );
|
|
|
|
|
|
|
|
mw.eventLog.dispatch( transformed[ 0 ], transformed[ 1 ] );
|
2019-06-27 21:48:25 +00:00
|
|
|
}
|
|
|
|
} );
|
2018-10-26 20:48:55 +00:00
|
|
|
};
|
2019-06-27 21:48:25 +00:00
|
|
|
}
|
2015-04-19 23:47:37 +00:00
|
|
|
|
2022-12-16 20:46:11 +00:00
|
|
|
function addABTestData( data, addToken ) {
|
2022-02-11 21:41:53 +00:00
|
|
|
// DiscussionTools New Topic A/B test for logged out users
|
|
|
|
if ( !mw.config.get( 'wgDiscussionToolsABTest' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-19 06:08:37 +00:00
|
|
|
if ( mw.config.get( 'wgDiscussionToolsABTestBucket' ) ) {
|
|
|
|
data.bucket = mw.config.get( 'wgDiscussionToolsABTestBucket' );
|
2022-02-11 21:41:53 +00:00
|
|
|
}
|
2022-12-16 20:46:11 +00:00
|
|
|
if ( mw.user.isAnon() && addToken ) {
|
|
|
|
var token = mw.cookie.get( 'DTABid', '' );
|
|
|
|
if ( token ) {
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
data.anonymous_user_token = token;
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 21:41:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var actionPrefixMap = {
|
|
|
|
firstChange: 'first_change',
|
|
|
|
saveIntent: 'save_intent',
|
|
|
|
saveAttempt: 'save_attempt',
|
|
|
|
saveSuccess: 'save_success',
|
|
|
|
saveFailure: 'save_failure'
|
|
|
|
};
|
|
|
|
|
2022-11-11 14:32:24 +00:00
|
|
|
var logEditEvent = sampledLogger(
|
|
|
|
'EditAttemptStep',
|
|
|
|
function ( inSample, action, data ) {
|
|
|
|
var actionPrefix = actionPrefixMap[ action ] || action;
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
data = $.extend( {
|
|
|
|
version: 1,
|
|
|
|
action: action,
|
|
|
|
is_oversample: !inSample,
|
|
|
|
editing_session_id: editingSessionId,
|
|
|
|
page_token: mw.user.getPageviewToken(),
|
|
|
|
session_token: mw.user.sessionId(),
|
|
|
|
editor_interface: 'wikitext',
|
|
|
|
platform: 'desktop', // FIXME T249944
|
|
|
|
integration: 'page',
|
|
|
|
page_id: mw.config.get( 'wgArticleId' ),
|
|
|
|
page_title: mw.config.get( 'wgPageName' ),
|
|
|
|
page_ns: mw.config.get( 'wgNamespaceNumber' ),
|
|
|
|
revision_id: mw.config.get( 'wgRevisionId' ) || +$( 'input[name=parentRevId]' ).val() || 0,
|
|
|
|
user_id: mw.user.getId(),
|
|
|
|
user_editcount: mw.config.get( 'wgUserEditCount', 0 ),
|
|
|
|
mw_version: mw.config.get( 'wgVersion' )
|
|
|
|
}, data );
|
|
|
|
|
|
|
|
if ( mw.user.isAnon() ) {
|
|
|
|
data.user_class = 'IP';
|
|
|
|
}
|
2019-06-27 21:48:25 +00:00
|
|
|
|
2022-12-16 20:46:11 +00:00
|
|
|
addABTestData( data, true );
|
2019-06-27 21:48:25 +00:00
|
|
|
|
2022-11-11 14:32:24 +00:00
|
|
|
// Schema's kind of a mess of special properties
|
|
|
|
if ( data.action === 'init' || data.action === 'abort' || data.action === 'saveFailure' ) {
|
|
|
|
data[ actionPrefix + '_type' ] = data.type;
|
|
|
|
}
|
|
|
|
if ( data.action === 'init' || data.action === 'abort' ) {
|
|
|
|
data[ actionPrefix + '_mechanism' ] = data.mechanism;
|
|
|
|
}
|
|
|
|
if ( data.action !== 'init' ) {
|
|
|
|
data[ actionPrefix + '_timing' ] = data.timing === undefined ? 0 : Math.floor( data.timing );
|
|
|
|
}
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
|
|
|
|
// Remove renamed properties
|
|
|
|
delete data.type;
|
|
|
|
delete data.mechanism;
|
|
|
|
delete data.timing;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Transforms the data produced by the callback above into an event name/custom data pair
|
|
|
|
// to be logged via the Metrics Platform.
|
|
|
|
function ( data ) {
|
|
|
|
var actionPrefix = actionPrefixMap[ data.action ] || data.action,
|
|
|
|
eventName = 'eas.wt.' + actionPrefix,
|
|
|
|
customData = $.extend( {}, data );
|
|
|
|
|
|
|
|
delete customData.version;
|
|
|
|
delete customData.action;
|
|
|
|
|
|
|
|
// Sampling rate (and therefore whether a stream should oversample) is captured in
|
|
|
|
// the stream config ($wgEventStreams).
|
|
|
|
delete customData.is_oversample;
|
|
|
|
|
|
|
|
delete customData.page_token;
|
|
|
|
delete customData.session_token;
|
|
|
|
|
|
|
|
// Platform can be derived from the agent_client_platform_family context attribute
|
|
|
|
// mixed in by the JavaScript Metrics Platform Client. The context attribute will be
|
|
|
|
// "desktop_browser" or "mobile_browser" depending on whether the MobileFrontend
|
|
|
|
// extension has signalled that it is enabled.
|
|
|
|
delete customData.platform;
|
|
|
|
|
|
|
|
delete customData.page_id;
|
|
|
|
delete customData.page_title;
|
|
|
|
delete customData.page_ns;
|
|
|
|
|
|
|
|
// If the revision ID can be fetched from the config, then it will be mixed in by the
|
|
|
|
// JavaScript Metrics Platform Client.
|
|
|
|
if ( mw.config.exists( 'wgRevisionId' ) ) {
|
|
|
|
delete customData.revision_id;
|
|
|
|
}
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2022-11-11 14:32:24 +00:00
|
|
|
delete customData.user_id;
|
|
|
|
delete customData.user_editcount;
|
|
|
|
delete customData.mw_version;
|
2021-01-29 23:25:19 +00:00
|
|
|
|
2022-11-11 14:32:24 +00:00
|
|
|
return [ eventName, customData ];
|
2019-09-10 14:34:35 +00:00
|
|
|
}
|
2022-11-11 14:32:24 +00:00
|
|
|
);
|
2018-10-26 20:48:55 +00:00
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var logEditFeature = sampledLogger( 'VisualEditorFeatureUse', function ( inSample, feature, action ) {
|
2020-06-08 17:05:19 +00:00
|
|
|
/* eslint-disable camelcase */
|
2021-01-29 23:25:19 +00:00
|
|
|
var data = {
|
2019-06-27 21:48:25 +00:00
|
|
|
feature: feature,
|
|
|
|
action: action,
|
2020-06-08 17:05:19 +00:00
|
|
|
editingSessionId: editingSessionId,
|
|
|
|
user_id: mw.user.getId(),
|
|
|
|
user_editcount: mw.config.get( 'wgUserEditCount', 0 ),
|
|
|
|
platform: 'desktop', // FIXME T249944
|
|
|
|
integration: 'page',
|
|
|
|
editor_interface: 'wikitext'
|
2019-06-27 21:48:25 +00:00
|
|
|
};
|
2022-02-11 21:41:53 +00:00
|
|
|
addABTestData( data );
|
2022-02-02 21:10:06 +00:00
|
|
|
/* eslint-enable camelcase */
|
2021-01-29 23:25:19 +00:00
|
|
|
return data;
|
2019-06-27 21:48:25 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
function logAbort( switchingToVE, unmodified ) {
|
|
|
|
if ( switchingToVE ) {
|
|
|
|
logEditFeature( 'editor-switch', 'visual-desktop' );
|
|
|
|
}
|
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var abortType;
|
2019-06-27 21:48:25 +00:00
|
|
|
if ( switchingToVE && unmodified ) {
|
|
|
|
abortType = 'switchnochange';
|
|
|
|
} else if ( switchingToVE ) {
|
|
|
|
abortType = 'switchwithout';
|
|
|
|
} else if ( unmodified ) {
|
|
|
|
abortType = 'nochange';
|
|
|
|
} else {
|
|
|
|
abortType = 'abandon';
|
|
|
|
}
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2019-06-27 21:48:25 +00:00
|
|
|
logEditEvent( 'abort', {
|
|
|
|
type: abortType
|
2015-03-24 21:14:16 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
$( function () {
|
|
|
|
var $textarea = $( '#wpTextbox1' ),
|
2015-04-19 23:47:37 +00:00
|
|
|
$editingSessionIdInput = $( '#editingStatsId' ),
|
2021-06-15 14:45:13 +00:00
|
|
|
origText = $textarea.val();
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2020-04-07 10:50:44 +00:00
|
|
|
// T263505, T249038
|
|
|
|
$( '#wikieditorUsed' ).val( 'yes' );
|
2021-04-30 22:45:29 +00:00
|
|
|
|
2015-04-19 23:47:37 +00:00
|
|
|
if ( $editingSessionIdInput.length ) {
|
|
|
|
editingSessionId = $editingSessionIdInput.val();
|
2017-12-11 17:46:47 +00:00
|
|
|
if ( window.performance && window.performance.timing ) {
|
|
|
|
// We want to track from the time the user started to try to
|
|
|
|
// launch the editor which navigationStart approximates. All
|
|
|
|
// of our supported browsers *should* allow this. Rather than
|
|
|
|
// fall back to the timestamp when the page loaded for those
|
|
|
|
// that don't, we just ignore them, so as to not skew the
|
|
|
|
// results towards better-performance in those cases.
|
2021-06-15 14:45:13 +00:00
|
|
|
var readyTime = Date.now();
|
2017-12-11 17:46:47 +00:00
|
|
|
logEditEvent( 'ready', {
|
2019-09-10 14:34:35 +00:00
|
|
|
timing: readyTime - window.performance.timing.navigationStart
|
2017-12-06 16:31:52 +00:00
|
|
|
} );
|
2017-12-11 17:46:47 +00:00
|
|
|
$textarea.on( 'wikiEditor-toolbar-doneInitialSections', function () {
|
|
|
|
logEditEvent( 'loaded', {
|
|
|
|
timing: Date.now() - window.performance.timing.navigationStart
|
|
|
|
} );
|
2019-09-10 14:34:35 +00:00
|
|
|
} ).one( 'input', function () {
|
|
|
|
logEditEvent( 'firstChange', {
|
|
|
|
timing: Date.now() - readyTime
|
|
|
|
} );
|
2017-12-11 17:46:47 +00:00
|
|
|
} );
|
|
|
|
}
|
2021-11-03 05:33:38 +00:00
|
|
|
var $form = $textarea.closest( 'form' );
|
|
|
|
if ( mw.user.options.get( 'uselivepreview' ) ) {
|
|
|
|
$form.find( '#wpPreview' ).on( 'click', function () {
|
|
|
|
logEditFeature( 'preview', 'preview-live' );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var submitting;
|
2021-11-03 05:33:38 +00:00
|
|
|
$form.on( 'submit', function () {
|
2015-03-24 21:14:16 +00:00
|
|
|
submitting = true;
|
|
|
|
} );
|
2021-06-15 14:45:13 +00:00
|
|
|
var onUnloadFallback = window.onunload;
|
2019-06-27 21:48:25 +00:00
|
|
|
|
2015-03-24 21:14:16 +00:00
|
|
|
window.onunload = function () {
|
2021-06-15 14:45:13 +00:00
|
|
|
var unmodified = mw.config.get( 'wgAction' ) !== 'submit' && origText === $textarea.val(),
|
2015-11-15 14:30:18 +00:00
|
|
|
caVeEdit = $( '#ca-ve-edit' )[ 0 ],
|
2015-04-20 01:13:50 +00:00
|
|
|
switchingToVE = caVeEdit && (
|
|
|
|
document.activeElement === caVeEdit ||
|
|
|
|
$.contains( caVeEdit, document.activeElement )
|
2019-06-27 21:48:25 +00:00
|
|
|
);
|
2015-03-24 21:14:16 +00:00
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var fallbackResult;
|
2015-03-24 21:14:16 +00:00
|
|
|
if ( onUnloadFallback ) {
|
|
|
|
fallbackResult = onUnloadFallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !submitting ) {
|
2019-06-27 21:48:25 +00:00
|
|
|
logAbort( switchingToVE, unmodified );
|
2015-03-24 21:14:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 23:47:37 +00:00
|
|
|
// If/when the user uses the back button to go back to the edit form
|
|
|
|
// and the browser serves this from bfcache, regenerate the session ID
|
|
|
|
// so we don't use the same ID twice. Ideally we'd do this by listening to the pageshow
|
|
|
|
// event and checking e.originalEvent.persisted, but that doesn't work in Chrome:
|
|
|
|
// https://code.google.com/p/chromium/issues/detail?id=344507
|
|
|
|
// So instead we modify the DOM here, after sending the abort event.
|
|
|
|
editingSessionId = mw.user.generateRandomSessionId();
|
|
|
|
$editingSessionIdInput.val( editingSessionId );
|
|
|
|
|
2015-03-24 21:14:16 +00:00
|
|
|
return fallbackResult;
|
|
|
|
};
|
2019-06-27 21:48:25 +00:00
|
|
|
$textarea.on( 'wikiEditor-switching-visualeditor', function () {
|
|
|
|
var unmodified = mw.config.get( 'wgAction' ) !== 'submit' && origText === $textarea.val();
|
|
|
|
// A non-navigation switch to VE has occurred. As such, avoid eventually
|
|
|
|
// double-logging an abort when VE is done.
|
|
|
|
window.onunload = onUnloadFallback;
|
|
|
|
|
|
|
|
logAbort( true, unmodified );
|
|
|
|
} );
|
2022-11-17 15:39:43 +00:00
|
|
|
|
|
|
|
// Add logging for Realtime Preview.
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.enable' ).add( function () {
|
|
|
|
logEditFeature( 'preview', 'preview-realtime-on' );
|
|
|
|
} );
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.inuse' ).add( function () {
|
|
|
|
logEditFeature( 'preview', 'preview-realtime-inuse' );
|
|
|
|
} );
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.disable' ).add( function () {
|
|
|
|
logEditFeature( 'preview', 'preview-realtime-off' );
|
|
|
|
} );
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.loaded' ).add( function () {
|
|
|
|
logEditFeature( 'preview', 'preview-realtime-loaded' );
|
|
|
|
} );
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.stop' ).add( function () {
|
|
|
|
logEditFeature( 'preview', 'preview-realtime-error-stopped' );
|
|
|
|
} );
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.reloadError' ).add( function () {
|
|
|
|
logEditFeature( 'preview', 'preview-realtime-reload-error' );
|
|
|
|
} );
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.reloadHover' ).add( function () {
|
|
|
|
logEditFeature( 'preview', 'preview-realtime-reload-hover' );
|
|
|
|
} );
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.reloadManual' ).add( function () {
|
|
|
|
logEditFeature( 'preview', 'preview-realtime-reload-manual' );
|
|
|
|
} );
|
2015-03-24 21:14:16 +00:00
|
|
|
}
|
2019-07-20 08:09:04 +00:00
|
|
|
|
|
|
|
// The old toolbar is still in place and needs to be removed so there aren't two toolbars
|
|
|
|
$( '#toolbar' ).remove();
|
|
|
|
// Add toolbar module
|
|
|
|
// TODO: Implement .wikiEditor( 'remove' )
|
2021-01-19 12:56:23 +00:00
|
|
|
mw.addWikiEditor( $textarea );
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.addWikiEditor = function ( $textarea ) {
|
2022-03-06 08:56:47 +00:00
|
|
|
if ( $textarea.css( 'display' ) === 'none' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-20 08:09:04 +00:00
|
|
|
$textarea.wikiEditor(
|
|
|
|
'addModule', require( './jquery.wikiEditor.toolbar.config.js' )
|
|
|
|
);
|
|
|
|
|
2021-06-15 14:45:13 +00:00
|
|
|
var dialogsConfig = require( './jquery.wikiEditor.dialogs.config.js' );
|
2019-07-20 08:09:04 +00:00
|
|
|
// Replace icons
|
|
|
|
dialogsConfig.replaceIcons( $textarea );
|
|
|
|
// Add dialogs module
|
|
|
|
$textarea.wikiEditor( 'addModule', dialogsConfig.getDefaultConfig() );
|
2021-01-19 12:56:23 +00:00
|
|
|
|
|
|
|
};
|
2022-03-29 05:34:11 +00:00
|
|
|
|
2018-11-12 22:28:40 +00:00
|
|
|
}() );
|