-
Notifications
You must be signed in to change notification settings - Fork 1
/
universalLanguageSelector.js
341 lines (298 loc) · 8.68 KB
/
universalLanguageSelector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*!
* ULS Event logger
*
* See https://meta.wikimedia.org/wiki/Schema:UniversalLanguageSelector
*
* @since 2013.08
*
* Copyright (C) 2012-2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
* Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other
* contributors. See CREDITS for a list.
*
* UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't
* have to do anything special to choose one license or the other and you don't
* have to notify anyone which license you are using. You are free to use
* UniversalLanguageSelector in commercial projects as long as the copyright
* header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
*
* @licence GNU General Public Licence 2.0 or later
* @licence MIT License
*/
'use strict';
// ---
//
// The following functions and data are used time how long the page is visible before the user
// changed their interface language (see `interfaceLanguageChange()`).
//
// See also Stephane Bisson's implementation in WikimediaEvents/ext.wikimediaEvents/InukaPageView.js
// and associated collaborative design/discussion at https://gerrit.wikimedia.org/r/c/mediawiki/extensions/WikimediaEvents/+/551259.
const startedAt = mw.now();
let hiddenAt = null;
let timeHidden = 0;
function onHide() {
if ( !hiddenAt ) {
hiddenAt = mw.now();
}
}
function onShow() {
if ( hiddenAt ) {
timeHidden += mw.now() - hiddenAt;
hiddenAt = null;
}
}
// ---
const skin = mw.config.get( 'skin' );
let skinVersion;
/**
* @return {string|null} If the user is using the Vector skin, then `'legacy'` or `'latest'`;
* `null` otherwise
*/
function getSkinVersion() {
if ( skinVersion === undefined ) {
if ( [ 'vector', 'vector-2022' ].indexOf( String( skin ) ) > -1 ) {
skinVersion = document.body.classList.contains( 'skin-vector-legacy' ) ? 'legacy' : 'latest';
} else {
skinVersion = null;
}
}
return skinVersion;
}
/**
* Try to emit an EventLogging event with schema 'UniversalLanguageSelector'.
*
* If EventLogging is not installed, this simply does nothing.
*
* @param {Object} event Event action and optional fields
*/
function log( event ) {
event = Object.assign( {
// Note well that the version and token properties _could_ be removed as they've both been
// superseded: version has been superseded by the move to the Modern Event Platform; and
// token has been superseded by the use of the web_session_id property from the
// web_identifiers fragment below. As of 2021/03/22, however, they can't be removed as
// backwards incompatible changes may cause issues with the Hive ingestion process (see
// the discussion on https://gerrit.wikimedia.org/r/c/schemas/event/secondary/+/668743 for
// detail).
version: 3,
token: '',
// ---
contentLanguage: mw.config.get( 'wgContentLanguage' ),
interfaceLanguage: mw.config.get( 'wgUserLanguage' ),
// The following properties were added in https://phabricator.wikimedia.org/T275766.
// For detail about the web_session_id property, see
// https://schema.wikimedia.org/repositories/secondary/jsonschema/fragment/analytics/web_identifiers/current.yaml.
web_session_id: mw.user.sessionId(),
isAnon: mw.user.isAnon(),
skin: skin,
skinVersion: getSkinVersion()
}, event );
const userEditBucket = mw.config.get( 'wgUserEditCountBucket' );
if ( userEditBucket ) {
event.userEditBucket = userEditBucket;
}
mw.track( 'event.UniversalLanguageSelector', event );
}
/**
* Log language settings open
*
* @param {string} context Where it was opened from
*/
function ulsSettingsOpen( context ) {
log( {
action: 'settings-open',
context: context
} );
}
/**
* Log when the compact language links treatment is opened.
*
* @param {jQuery} $trigger The element that triggered the opening of the compact language links
* dialog
*/
function ulsCompactLanguageLinksOpen( $trigger ) {
let context = 'other';
const isVector = [ 'vector', 'vector-2022' ].indexOf( String( skin ) ) > -1;
if ( isVector && $trigger.is( '#p-lang-btn-checkbox' ) ) {
context = 'header';
} else if ( isVector && $trigger.is( '#p-lang-btn-sticky-header' ) ) {
context = 'sticky-header';
}
log( {
action: 'compact-language-links-open',
context: context
} );
}
/**
* Log language revert
*/
function ulsLanguageRevert() {
log( { action: 'ui-lang-revert' } );
}
/**
* Log IME disabling
*
* @param {string} context Where the setting was changed.
*/
function disableIME( context ) {
log( { action: 'ime-disable', context: context } );
}
/**
* Log IME enabling
*
* @param {string} context Where the setting was changed.
*/
function enableIME( context ) {
log( { action: 'ime-enable', context: context } );
}
/**
* Log IME change
*
* @param {string} inputMethod
*/
function changeIME( inputMethod ) {
log( {
action: 'ime-change',
inputMethod: inputMethod
} );
}
/**
* Log login link click in display settings.
*/
function loginClick() {
log( { action: 'login-click' } );
}
/**
* Log when "More languages" item in IME menu is clicked.
*/
function imeMoreLanguages() {
log( {
action: 'more-languages-access',
context: 'ime'
} );
}
/**
* Log interface language change
*
* @param {string} language language code
* @param {string} [source] symbolic name for source
*/
function interfaceLanguageChange( language, source ) {
const logParams = {
action: 'language-change',
context: source || 'interface',
selectedInterfaceLanguage: language,
// The number of milliseconds that the page was visible before the user changed their
// interface language.
//
// Since `mw.now()` is used, this could be a floating-point value with microsecond
// precision.
timeToChangeLanguage: mw.now() - startedAt - timeHidden
};
log( logParams );
}
/**
* More languages in display settings is clicked
*/
function interfaceMoreLanguages() {
log( {
action: 'more-languages-access',
context: 'interface'
} );
}
/**
* Log font preference changes
*
* @param {string} context Either 'interface' or 'content'
* @param {string} language
* @param {string} font
*/
function fontChange( context, language, font ) {
const logParams = {
action: 'font-change',
context: context
};
if ( context === 'interface' ) {
logParams.interfaceFont = font;
logParams.selectedInterfaceLanguage = language;
} else {
logParams.contentFont = font;
}
log( logParams );
}
/**
* Log webfonts disabling
*
* @param {string} context Where the setting was changed.
*/
function disableWebfonts( context ) {
log( { action: 'webfonts-disable', context: context } );
}
/**
* Log webfonts enabling
*
* @param {string} context Where the setting was changed.
*/
function enableWebfonts( context ) {
log( { action: 'webfonts-enable', context: context } );
}
/**
* Log search strings which produce no search results.
*
* @param {jQuery.event} event The original event
* @param {Object} data Information about the failed search
*/
function noSearchResults( event, data ) {
log( {
action: 'no-search-results',
context: data.query,
ulsPurpose: data.ulsPurpose,
title: mw.config.get( 'wgPageName' )
} );
}
/**
* Start listening for event logging
*/
function listen() {
// Register handlers for event logging triggers
mw.hook( 'mw.uls.settings.open' ).add( ulsSettingsOpen );
mw.hook( 'mw.uls.compact_language_links.open' ).add( ulsCompactLanguageLinksOpen );
mw.hook( 'mw.uls.language.revert' ).add( ulsLanguageRevert );
mw.hook( 'mw.uls.ime.enable' ).add( enableIME );
mw.hook( 'mw.uls.ime.disable' ).add( disableIME );
mw.hook( 'mw.uls.ime.change' ).add( changeIME );
mw.hook( 'mw.uls.login.click' ).add( loginClick );
mw.hook( 'mw.uls.ime.morelanguages' ).add( imeMoreLanguages );
mw.hook( 'mw.uls.interface.morelanguages' ).add( interfaceMoreLanguages );
mw.hook( 'mw.uls.interface.language.change' ).add( interfaceLanguageChange );
mw.hook( 'mw.uls.font.change' ).add( fontChange );
mw.hook( 'mw.uls.webfonts.enable' ).add( enableWebfonts );
mw.hook( 'mw.uls.webfonts.disable' ).add( disableWebfonts );
$( document.body ).on(
'noresults.uls',
'.uls-menu .uls-languagefilter',
noSearchResults
);
// Time how long the page is visible.
if ( document.hidden ) {
onHide();
}
document.addEventListener( 'visibilitychange', () => {
if ( document.hidden ) {
onHide();
} else {
onShow();
}
} );
// Wait for DOMContentLoaded to ensure #p-lang is present.
$( () => {
// Log language switching event in legacy sidebar for modern Vector.
// eslint-disable-next-line no-jquery/no-global-selector
$( '.mw-workspace-container #p-lang' ).on( 'click', '.interlanguage-link-target', function () {
mw.hook( 'mw.uls.interface.language.change' ).fire(
this.attributes.getNamedItem( 'hreflang' ).value,
'languages-list'
);
} );
} );
}
listen();