-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.js
324 lines (293 loc) · 9.65 KB
/
core.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
/*!
* @module ext.eventLogging.core
* @author Ori Livneh <ori@wikimedia.org>
*/
( function ( mw, $ ) {
'use strict';
var self, baseUrl, debugMode;
// `baseUrl` corresponds to $wgEventLoggingBaseUri, as declared
// in EventLogging.php. If the default value of 'false' has not
// been overridden, events will not be sent to the server.
baseUrl = mw.config.get( 'wgEventLoggingBaseUri' );
// Support both 1 or "1" (T54542)
debugMode = Number( mw.user.options.get( 'eventlogging-display-web' ) ) === 1;
/**
* Client-side EventLogging API.
*
* The main API is `mw.eventLog.logEvent`. Most other methods represent internal
* functionality, which is exposed only to ease debugging code and writing tests.
*
* Instances of `ResourceLoaderSchemaModule` indicate a dependency on this module and
* declare themselves via the declareSchema method.
*
* Developers should not load this module directly, but work with schema modules instead.
* Schema modules will load this module as a dependency.
*
* @private
* @class mw.eventLog.Core
* @singleton
*/
self = {
/**
* Schema registry. Schemas that have been declared explicitly via
* `eventLog.declareSchema` or implicitly by being referenced in an
* `eventLog.logEvent` call are stored in this object.
*
* @private
* @property schemas
* @type Object
*/
schemas: {},
/**
* Maximum length in chars that a beacon URL can have.
* Relevant:
*
* - Length that browsers support (http://stackoverflow.com/a/417184/319266)
* - Length that proxies support (e.g. Varnish)
* - varnishlog (shm_reclen)
* - varnishkafka
*
* @private
* @property maxUrlSize
* @type Number
*/
maxUrlSize: 2000,
/**
* Load a schema from the schema registry.
* If the schema does not exist, it will be initialised.
*
* @private
* @param {string} schemaName Name of schema.
* @return {Object} Schema object.
*/
getSchema: function ( schemaName ) {
if ( !Object.hasOwnProperty.call( self.schemas, schemaName ) ) {
self.schemas[ schemaName ] = { schema: { title: schemaName } };
}
return self.schemas[ schemaName ];
},
/**
* Register a schema so that it can be used to validate events.
* `ResourceLoaderSchemaModule` instances generate JavaScript code that
* invokes this method.
*
* @private
* @param {string} schemaName Name of schema.
* @param {Object} meta An object describing a schema:
* @param {number} meta.revision Revision ID.
* @param {Object} meta.schema The schema itself.
* @return {Object} The registered schema.
*/
declareSchema: function ( schemaName, meta ) {
return $.extend( true, self.getSchema( schemaName ), meta );
},
/**
* Checks whether a JavaScript value conforms to a specified
* JSON Schema type.
*
* @private
* @param {Object} value Object to test.
* @param {string} type JSON Schema type.
* @return {boolean} Whether value is instance of type.
*/
isInstanceOf: function ( value, type ) {
var jsType = $.type( value );
switch ( type ) {
case 'integer':
return jsType === 'number' && value % 1 === 0;
case 'number':
return jsType === 'number' && isFinite( value );
case 'timestamp':
return jsType === 'date' || ( jsType === 'number' && value >= 0 && value % 1 === 0 );
default:
return jsType === type;
}
},
/**
* Check whether a JavaScript object conforms to a JSON Schema.
*
* @private
* @param {Object} obj Object to validate.
* @param {Object} schema JSON Schema object.
* @return {Array} An array of validation errors (empty if valid).
*/
validate: function ( obj, schema ) {
var key, val, prop,
errors = [];
if ( !schema || !schema.properties ) {
errors.push( 'Missing or empty schema' );
return errors;
}
for ( key in obj ) {
if ( !Object.hasOwnProperty.call( schema.properties, key ) ) {
errors.push( mw.format( 'Undeclared property "$1"', key ) );
}
}
for ( key in schema.properties ) {
prop = schema.properties[ key ];
if ( !Object.hasOwnProperty.call( obj, key ) ) {
if ( prop.required ) {
errors.push( mw.format( 'Missing property "$1"', key ) );
}
continue;
}
val = obj[ key ];
if ( !( self.isInstanceOf( val, prop.type ) ) ) {
errors.push( mw.format(
'Value $1 is the wrong type for property "$2" ($3 expected)',
JSON.stringify( val ), key, prop.type
) );
continue;
}
if ( prop.enum && $.inArray( val, prop.enum ) === -1 ) {
errors.push( mw.format(
'Value $1 for property "$2" is not one of $3',
JSON.stringify( val ), key, JSON.stringify( prop.enum )
) );
}
}
return errors;
},
/**
* Sets default property values for events belonging to a particular schema.
* If default values have already been declared, the new defaults are merged
* on top.
*
* @param {string} schemaName The name of the schema.
* @param {Object} schemaDefaults A map of property names to default values.
* @return {Object} Combined defaults for schema.
*/
setDefaults: function ( schemaName, schemaDefaults ) {
return self.declareSchema( schemaName, { defaults: schemaDefaults } );
},
/**
* Prepares an event for dispatch by filling defaults for any missing
* properties and by encapsulating the event object in an object which
* contains metadata about the event itself.
*
* @private
* @param {string} schemaName Canonical schema name.
* @param {Object} eventData Event instance.
* @return {Object} Encapsulated event.
*/
prepare: function ( schemaName, eventData ) {
var schema = self.getSchema( schemaName ),
event = $.extend( true, {}, schema.defaults, eventData ),
errors = self.validate( event, schema.schema );
while ( errors.length ) {
mw.track( 'eventlogging.error', mw.format( '[$1] $2', schemaName, errors.pop() ) );
}
return {
event: event,
revision: schema.revision || -1,
schema: schemaName,
webHost: location.hostname,
wiki: mw.config.get( 'wgDBname' )
};
},
/**
* Constructs the EventLogging URI based on the base URI and the
* encoded and stringified data.
*
* @private
* @param {Object} data Payload to send
* @return {string|boolean} The URI to log the event.
*/
makeBeaconUrl: function ( data ) {
var queryString = encodeURIComponent( JSON.stringify( data ) );
return baseUrl + '?' + queryString + ';';
},
/**
* Checks whether a beacon url is short enough,
* so that it does not get truncated by varnishncsa.
*
* @private
* @param {string} schemaName Canonical schema name.
* @param {string} url Beacon url.
* @return {string|undefined} The error message in case of error.
*/
checkUrlSize: function ( schemaName, url ) {
var message;
if ( url.length > self.maxUrlSize ) {
message = 'Url exceeds maximum length';
mw.eventLog.logFailure( schemaName, 'urlSize' );
mw.track( 'eventlogging.error', mw.format( '[$1] $2', schemaName, message ) );
return message;
}
},
/**
* Transfer data to a remote server by making a lightweight HTTP
* request to the specified URL.
*
* If the user expressed a preference not to be tracked, or if
* $wgEventLoggingBaseUri is unset, this method is a no-op.
*
* See https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack
*
* @param {string} url URL to request from the server.
* @return undefined
*/
sendBeacon: (
// Support: Firefox < 32 (yes/no)
/1|yes/.test( navigator.doNotTrack ) ||
// Support: IE 11, Safari 7.1.3+ (window.doNotTrack)
window.doNotTrack === '1' ||
// Support: IE 9, IE 10 (navigator.msDoNotTrack)
navigator.msDoNotTrack === '1' ||
!baseUrl
) ?
$.noop :
navigator.sendBeacon ?
function ( url ) { try { navigator.sendBeacon( url ); } catch ( e ) {} } :
function ( url ) { document.createElement( 'img' ).src = url; },
/**
* Construct and transmit to a remote server a record of some event
* having occurred. Events are represented as JavaScript objects that
* conform to a JSON Schema. The schema describes the properties the
* event object may (or must) contain and their type. This method
* represents the public client-side API of EventLogging.
*
* @param {string} schemaName Canonical schema name.
* @param {Object} eventData Event object.
* @return {jQuery.Promise} jQuery Promise object for the logging call.
*/
logEvent: function ( schemaName, eventData ) {
var event = self.prepare( schemaName, eventData ),
url = self.makeBeaconUrl( event ),
sizeError = self.checkUrlSize( schemaName, url ),
deferred = $.Deferred();
if ( !sizeError ) {
self.sendBeacon( url );
if ( debugMode ) {
mw.track( 'eventlogging.debug', event );
}
deferred.resolveWith( event, [ event ] );
} else {
deferred.rejectWith( event, [ event, sizeError ] );
}
return deferred.promise();
},
/**
* Increment the error count in statsd for this schema.
*
* Should be called instead of logEvent in case of an error.
*
* @param {string} schemaName
* @param {string} errorCode
*/
logFailure: function ( schemaName, errorCode ) {
// Record this failure as a simple counter. By default "counter.*" goes nowhere.
// The WikimediaEvents extension sends it to statsd.
mw.track( 'counter.eventlogging.client_errors.' + schemaName + '.' + errorCode );
}
};
// Output validation errors to the browser console, if available.
mw.trackSubscribe( 'eventlogging.error', function ( topic, error ) {
mw.log.error( error );
} );
/**
* @class mw.eventLog
* @mixins mw.eventLog.Core
*/
$.extend( mw.eventLog, self );
}( mediaWiki, jQuery ) );