summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UniversalLanguageSelector/resources/js/ext.uls.eventlogger.js
blob: 1c94f60c54c0cc5aee7134e816340e515d942a35 (plain)
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
/*!
 * ULS Event logger
 *
 * 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.
 *
 * @file
 * @ingroup Extensions
 * @licence GNU General Public Licence 2.0 or later
 * @licence MIT License
 */

( function () {
	'use strict';

	/**
	 * ULS Event logger
	 * See https://meta.wikimedia.org/wiki/Schema:UniversalLanguageSelector
	 *
	 * @since 2013.08
	 */
	function ULSEventLogger() {
		this.eventDefault = {
			version: 1,
			token: mw.user.id(),
			contentLanguage: mw.config.get( 'wgContentLanguage' ),
			interfaceLanguage: mw.config.get( 'wgUserLanguage' )
		};
		this.schemaDefault = 'UniversalLanguageSelector';
		this.listen();
	}

	ULSEventLogger.prototype = {
		/**
		 * Local wrapper for 'mw.eventLog.logEvent'
		 *
		 * @param {Object} event Event action and optional fields
		 * @param {string} schema The schema; 'UniversalLanguageSelector' is the default
		 * @return {jQuery.Promise} jQuery Promise object for the logging call
		 */
		log: function ( event, schema ) {
			// FIXME: We need to create our own deferred for two reasons:
			//  - logEvent might not be executed immediately
			//  - we cannot reject a promise returned by it
			// So we proxy the original promises status updates.
			var deferred = $.Deferred();

			schema = schema || this.schemaDefault;

			if ( schema === this.schemaDefault ) {
				event = $.extend( {}, this.eventBase, event );
			}

			mw.eventLog.logEvent( schema, event )
				.done( deferred.resolve )
				.fail( deferred.reject );

			return deferred.promise();
		},

		/**
		 * Listen for event logging
		 */
		listen: function () {
			// Register handlers for event logging triggers
			mw.hook( 'mw.uls.settings.open' ).add( this.ulsSettingsOpen.bind( this ) );
			mw.hook( 'mw.uls.language.revert' ).add( this.ulsLanguageRevert.bind( this ) );
			mw.hook( 'mw.uls.ime.enable' ).add( this.enableIME.bind( this ) );
			mw.hook( 'mw.uls.ime.disable' ).add( this.disableIME.bind( this ) );
			mw.hook( 'mw.uls.ime.change' ).add( this.changeIME.bind( this ) );
			mw.hook( 'mw.uls.login.click' ).add( this.loginClick.bind( this ) );
			mw.hook( 'mw.uls.ime.morelanguages' ).add( this.imeMoreLanguages.bind( this ) );
			mw.hook( 'mw.uls.interface.morelanguages' ).add( this.interfaceMoreLanguages.bind( this ) );
			mw.hook( 'mw.uls.interface.language.change' ).add( this.interfaceLanguageChange.bind( this ) );
			mw.hook( 'mw.uls.font.change' ).add( this.fontChange.bind( this ) );
			mw.hook( 'mw.uls.webfonts.enable' ).add( this.enableWebfonts.bind( this ) );
			mw.hook( 'mw.uls.webfonts.disable' ).add( this.disableWebfonts.bind( this ) );

			$( 'body' ).on( 'noresults.uls', '.uls-menu .uls-languagefilter',
				this.noSearchResults.bind( this )
			);
		},

		/**
		 * Log language settings open
		 *
		 * @param {string} context Where it was opened from
		 */
		ulsSettingsOpen: function ( context ) {
			this.log( {
				action: 'settings-open',
				context: context
			} );
		},

		/**
		 * Log language revert
		 *
		 * @param {jQuery.Deferred} deferred
		 */
		ulsLanguageRevert: function ( deferred ) {
			this.log( { action: 'ui-lang-revert' } ).always( deferred.resolve() );
		},

		/**
		 * Log IME disabling
		 *
		 * @param {string} context Where the setting was changed.
		 */
		disableIME: function ( context ) {
			this.log( { action: 'ime-disable', context: context } );
		},

		/**
		 * Log IME enabling
		 *
		 * @param {string} context Where the setting was changed.
		 */
		enableIME: function ( context ) {
			this.log( { action: 'ime-enable', context: context } );
		},

		/**
		 * Log IME change
		 *
		 * @param {string} inputMethod
		 */
		changeIME: function ( inputMethod ) {
			this.log( {
				action: 'ime-change',
				inputMethod: inputMethod
			} );
		},

		/**
		 * Log login link click in display settings.
		 *
		 * @param {jQuery.Deferred} deferred
		 */
		loginClick: function ( deferred ) {
			this.log( { action: 'login-click' } ).always( deferred.resolve );
		},

		/**
		 * More languages item in IME menu is clicked
		 */
		imeMoreLanguages: function () {
			this.log( {
				action: 'more-languages-access',
				context: 'ime'
			} );
		},

		/**
		 * Log interface language change
		 *
		 * @param {string} language language code
		 * @param {jQuery.Deferred} deferred
		 */
		interfaceLanguageChange: function ( language, deferred ) {
			var logParams = {
				action: 'language-change',
				context: 'interface',
				interfaceLanguage: language
			};

			this.log( logParams ).always( deferred.resolve );
		},

		/**
		 * More languages in display settings is clicked
		 */
		interfaceMoreLanguages: function () {
			this.log( {
				action: 'more-languages-access',
				context: 'interface'
			} );
		},

		/**
		 * Log font preference changes
		 *
		 * @param {string} context Either 'interface' or 'content'
		 * @param {string} language
		 * @param {string} font
		 */
		fontChange: function ( context, language, font ) {
			var logParams = {
				action: 'font-change',
				context: context
			};

			if ( context === 'interface' ) {
				$.extend( logParams, {
					interfaceFont: font,
					// Override in case the user changed the ui language but hasn't applied it yet
					interfaceLanguage: language
				} );
			} else {
				logParams.contentFont = font;
			}

			this.log( logParams );
		},

		/**
		 * Log webfonts disabling
		 *
		 * @param {string} context Where the setting was changed.
		 */
		disableWebfonts: function ( context ) {
			this.log( { action: 'webfonts-disable', context: context } );
		},

		/**
		 * Log webfonts enabling
		 *
		 * @param {string} context Where the setting was changed.
		 */
		enableWebfonts: function ( context ) {
			this.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
		 */
		noSearchResults: function ( event, data ) {
			this.log( {
				action: 'no-search-results',
				context: data.query,
				ulsPurpose: data.ulsPurpose,
				title: mw.config.get( 'wgPageName' )
			} );
		}
	};

	mw.uls = mw.uls || {};
	mw.uls.eventlogger = new ULSEventLogger();
}() );