2014-05-19 09:24:54 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-05-19 09:24:54 +00:00
|
|
|
var L;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class providing common code for EventLogging loggers
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-09-04 23:00:55 +00:00
|
|
|
* @class mw.mmv.logging.Logger
|
2014-05-19 09:24:54 +00:00
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
function Logger() {
|
|
|
|
this.Geo = undefined;
|
|
|
|
this.eventLog = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
L = Logger.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sampling factor key-value map.
|
|
|
|
*
|
|
|
|
* Makes the logger sample log events instead of recording each one if > 0. Disables logging if === 0.
|
2020-06-26 10:21:04 +00:00
|
|
|
*
|
2014-05-19 09:24:54 +00:00
|
|
|
* @property {number}
|
|
|
|
*/
|
|
|
|
L.samplingFactor = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EventLogging schema
|
2020-06-26 10:21:04 +00:00
|
|
|
*
|
2014-05-19 09:24:54 +00:00
|
|
|
* @property {string}
|
|
|
|
*/
|
|
|
|
L.schema = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Geo object providing country information about the visitor
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-05-19 09:24:54 +00:00
|
|
|
* @param {Object} Geo object containing country GeoIP information about the user
|
|
|
|
*/
|
|
|
|
L.setGeo = function ( Geo ) {
|
|
|
|
this.Geo = Geo;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the eventLog object providing a facility to record events
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-05-19 09:24:54 +00:00
|
|
|
* @param {mw.eventLog} eventLog EventLogging instance
|
|
|
|
*/
|
|
|
|
L.setEventLog = function ( eventLog ) {
|
|
|
|
this.eventLog = eventLog;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the dependencies that allow us to log events
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {jQuery.Promise}
|
2014-05-19 09:24:54 +00:00
|
|
|
*/
|
|
|
|
L.loadDependencies = function () {
|
|
|
|
var self = this,
|
|
|
|
waitForEventLog = $.Deferred();
|
|
|
|
|
|
|
|
// Waits for dom readiness because we don't want to have these dependencies loaded in the head
|
2017-01-06 15:04:56 +00:00
|
|
|
$( function () {
|
2014-05-19 09:24:54 +00:00
|
|
|
// window.Geo is currently defined in components that are loaded independently, there is no cheap
|
|
|
|
// way to load just that information. Either we piggy-back on something that already loaded it
|
|
|
|
// or we just don't have it
|
|
|
|
if ( window.Geo ) {
|
|
|
|
self.setGeo( window.Geo );
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-05-15 00:54:20 +00:00
|
|
|
mw.loader.using( 'ext.eventLogging', function () {
|
2014-05-19 09:24:54 +00:00
|
|
|
self.setEventLog( mw.eventLog );
|
|
|
|
waitForEventLog.resolve();
|
|
|
|
} );
|
|
|
|
} catch ( e ) {
|
|
|
|
waitForEventLog.reject();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return waitForEventLog;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not we should measure this request
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {boolean} True if this request needs to be sampled
|
2014-05-19 09:24:54 +00:00
|
|
|
*/
|
|
|
|
L.isInSample = function () {
|
2019-05-06 23:57:14 +00:00
|
|
|
if ( typeof this.samplingFactor !== 'number' || this.samplingFactor < 1 ) {
|
2014-05-19 09:24:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.floor( Math.random() * this.samplingFactor ) === 0;
|
|
|
|
};
|
|
|
|
|
2015-02-19 23:30:29 +00:00
|
|
|
/**
|
|
|
|
* Returns whether logging this event is enabled. This is intended for console logging, which
|
|
|
|
* (in debug mode) should be done even if the request is not being sampled, as long as logging
|
|
|
|
* is enabled for some sample.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {boolean} True if this logging is enabled
|
2015-02-19 23:30:29 +00:00
|
|
|
*/
|
|
|
|
L.isEnabled = function () {
|
2019-05-06 23:57:14 +00:00
|
|
|
return typeof this.samplingFactor === 'number' && this.samplingFactor >= 1;
|
2015-02-19 23:30:29 +00:00
|
|
|
};
|
|
|
|
|
2014-09-26 21:42:30 +00:00
|
|
|
/**
|
|
|
|
* True if the schema has a country field. Broken out in a separate function so it's easy to mock.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {boolean}
|
2014-09-26 21:42:30 +00:00
|
|
|
*/
|
|
|
|
L.schemaSupportsCountry = function () {
|
2019-05-15 00:54:20 +00:00
|
|
|
return this.eventLog && ( { // don't die if eventLog is a mock
|
|
|
|
// EventLogging only downloads schemas in debug mode, can't check for country dynamically
|
|
|
|
MediaViewer: false,
|
|
|
|
MultimediaViewerDimensions: false,
|
|
|
|
MultimediaViewerNetworkPerformance: true,
|
|
|
|
MultimediaViewerAttribution: false,
|
|
|
|
MultimediaViewerDuration: true
|
|
|
|
}[ this.schema ] || false );
|
2014-09-26 21:42:30 +00:00
|
|
|
};
|
|
|
|
|
2014-05-19 09:24:54 +00:00
|
|
|
/**
|
|
|
|
* Logs EventLogging data while including Geo data if any
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-05-19 09:24:54 +00:00
|
|
|
* @param {Object} data
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {jQuery.Promise}
|
2014-05-19 09:24:54 +00:00
|
|
|
*/
|
|
|
|
L.log = function ( data ) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if ( self.isInSample() ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
return this.loadDependencies().then( function () {
|
2014-05-19 09:24:54 +00:00
|
|
|
// Add Geo information if there's any
|
2014-09-26 21:42:30 +00:00
|
|
|
if (
|
|
|
|
self.Geo && self.Geo.country !== undefined &&
|
|
|
|
self.schemaSupportsCountry()
|
|
|
|
) {
|
2014-05-19 09:24:54 +00:00
|
|
|
data.country = self.Geo.country;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.eventLog.logEvent( self.schema, data );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
return $.Deferred().resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-04 23:00:55 +00:00
|
|
|
mw.mmv.logging = {};
|
|
|
|
mw.mmv.logging.Logger = Logger;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|