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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function ( mw, $ ) {
|
|
|
|
var L;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class providing common code for EventLogging loggers
|
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.
|
|
|
|
* @property {number}
|
|
|
|
*/
|
|
|
|
L.samplingFactor = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EventLogging schema
|
|
|
|
* @property {string}
|
|
|
|
*/
|
|
|
|
L.schema = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Geo object providing country information about the visitor
|
|
|
|
* @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
|
|
|
|
* @param {mw.eventLog} eventLog EventLogging instance
|
|
|
|
*/
|
|
|
|
L.setEventLog = function ( eventLog ) {
|
|
|
|
this.eventLog = eventLog;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the dependencies that allow us to log events
|
|
|
|
* @returns {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
$( document ).ready( function() {
|
|
|
|
// 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 {
|
|
|
|
mw.loader.using( [ 'ext.eventLogging', 'schema.' + self.schema ], function() {
|
|
|
|
self.setEventLog( mw.eventLog );
|
|
|
|
waitForEventLog.resolve();
|
|
|
|
} );
|
|
|
|
} catch ( e ) {
|
|
|
|
waitForEventLog.reject();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return waitForEventLog;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not we should measure this request
|
|
|
|
* @returns {boolean} True if this request needs to be sampled
|
|
|
|
*/
|
|
|
|
L.isInSample = function () {
|
|
|
|
if ( !$.isNumeric( this.samplingFactor ) || this.samplingFactor < 1 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.floor( Math.random() * this.samplingFactor ) === 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs EventLogging data while including Geo data if any
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
L.log = function ( data ) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if ( self.isInSample() ) {
|
|
|
|
return this.loadDependencies().then( function() {
|
|
|
|
// Add Geo information if there's any
|
|
|
|
if ( self.Geo && self.Geo.country !== undefined ) {
|
|
|
|
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;
|
2014-05-19 09:24:54 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|