Santhosh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/79047


Change subject: Eventlogger module for ULS
......................................................................

Eventlogger module for ULS

* Seperating event logging out of ULS functionality. Event logging
 is now losely coupled with ULS. ULS core modules can emit an event
 whereever eventlogging is required. The eventlogger module will act
 on the event if configured.
* Also introduce event emitter module that can act as a central event
 dispatching unit for ULS. For now used only for event logging. But in
 future can be used to coordinate preference changes from multiple entry
 points in UI and all other UI sync purposes.

Thanks to OO.js of VE for some part of eventemitter code.

Change-Id: I59dfcfb25c1acb85376b56239f2355ee7c4aff1e
---
M Resources.php
M UniversalLanguageSelector.hooks.php
A resources/js/ext.uls.eventemitter.js
A resources/js/ext.uls.eventlogger.js
M resources/js/ext.uls.interface.js
5 files changed, 219 insertions(+), 22 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UniversalLanguageSelector 
refs/changes/47/79047/1

diff --git a/Resources.php b/Resources.php
index 6ae9c6b..5044682 100644
--- a/Resources.php
+++ b/Resources.php
@@ -62,10 +62,20 @@
                'jquery.json',
                'jquery.uls',
                'ext.uls.i18n',
+               'ext.uls.eventemitter',
        ),
        'position' => 'top',
 ) + $resourcePaths;
 
+$wgResourceModules['ext.uls.eventemitter'] = array(
+       'scripts' => 'resources/js/ext.uls.eventemitter.js',
+) + $resourcePaths;
+
+$wgResourceModules['ext.uls.eventlogger'] = array(
+       'scripts' => 'resources/js/ext.uls.eventlogger.js',
+       'dependencies' => 'ext.uls.eventemitter',
+) + $resourcePaths;
+
 $wgResourceModules['ext.uls.i18n'] = array(
        'scripts' => 'resources/js/ext.uls.i18n.js',
        'dependencies' => 'jquery.i18n',
diff --git a/UniversalLanguageSelector.hooks.php 
b/UniversalLanguageSelector.hooks.php
index 0450ca1..a54adba 100644
--- a/UniversalLanguageSelector.hooks.php
+++ b/UniversalLanguageSelector.hooks.php
@@ -51,6 +51,7 @@
                // If EventLogging integration is enabled, load the schema 
module.
                if ( $wgULSEventLogging ) {
                        $out->addModules( 'schema.UniversalLanguageSelector' );
+                       $out->addModules( 'ext.uls.eventlogger' );
                }
 
                // If the extension is enabled, basic features (API, language 
data) available.
diff --git a/resources/js/ext.uls.eventemitter.js 
b/resources/js/ext.uls.eventemitter.js
new file mode 100644
index 0000000..3f26c5a
--- /dev/null
+++ b/resources/js/ext.uls.eventemitter.js
@@ -0,0 +1,87 @@
+/**
+ * ULS Event emitter
+ *
+ * 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 ( mw ) {
+
+'use strict';
+/**
+ * Event emitter.
+ * Simple event emitter implementation.
+ * @class EventEmitter
+ * @constructor
+ * @property {Object} bindings
+ */
+function EventEmitter () {
+       this.bindings = {};
+}
+/* Methods */
+
+/**
+ * Add a listener to events of a specific event.
+ *
+ * @method
+ * @param {string} event Type of event to listen to
+ * @param {Function} callback Function to call when event occurs
+ * @throws {Error} Listener argument is not a function or method name
+ * @chainable
+ */
+EventEmitter.prototype.on = function ( event, callback ) {
+       this.bindings[event] = this.bindings[event] || [];
+       // Validate callback
+       if ( typeof callback !== 'function' ) {
+               throw new Error( 'Invalid callback. Function or method name 
expected.' );
+       }
+       this.bindings[event].push( callback );
+       return this;
+};
+
+/**
+ * Adds a one-time listener to a specific event.
+ *
+ * @method
+ * @param {string} event Type of event to listen to
+ * @param {Function} listener Listener to call when event occurs
+ * @chainable
+ */
+EventEmitter.prototype.once = function ( event, callback ) {
+       var eventEmitter = this;
+       return this.on( event, function listenerWrapper() {
+               delete eventEmitter.bindings[event];
+               callback.apply( eventEmitter, Array.prototype.slice.call( 
arguments, 0 ) );
+       } );
+};
+
+/**
+ * Emit an event.
+ *
+ * @method
+ * @param {string} event Type of event
+ * @param {Mixed} args First in a list of variadic arguments passed to event 
handler (optional)
+ */
+EventEmitter.prototype.emit = function ( event, args ) {
+       this.bindings[event] = this.bindings[event] || [];
+       args = args || [];
+       this.bindings[event].forEach( function ( callback ) {
+               callback.apply( this, args );
+       } );
+};
+
+mw.uls = mw.uls || {};
+mw.uls.eventemitter = new EventEmitter();
+}( mediaWiki ) );
\ No newline at end of file
diff --git a/resources/js/ext.uls.eventlogger.js 
b/resources/js/ext.uls.eventlogger.js
new file mode 100644
index 0000000..13769ae
--- /dev/null
+++ b/resources/js/ext.uls.eventlogger.js
@@ -0,0 +1,114 @@
+/**
+ * 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 ( $, mw ) {
+'use strict';
+/**
+ * ULS Event logger
+ *
+ * @since 2013.07
+ * @see https://meta.wikimedia.org/wiki/Schema:UniversalLanguageSelector
+ */
+function ULSEventLogger ( ) {
+       this.logEventQueue = $.Callbacks( 'memory once' );
+       this.init();
+       this.listen();
+}
+
+ULSEventLogger.prototype = {
+       init: function() {
+               var eventLogger = this;
+               // If EventLogging integration is enabled, set event defaults 
and make the
+               // the function call event logging with correct schema.
+               if ( mw.config.get( 'wgULSEventLogging' ) ) {
+                       mw.loader.using( 'schema.UniversalLanguageSelector', 
function () {
+                               mw.eventLog.setDefaults( 
'UniversalLanguageSelector', {
+                                       version: 1,
+                                       token: mw.user.id(),
+                                       contentLanguage: mw.config.get( 
'wgContentLanguage' ),
+                                       interfaceLanguage: mw.config.get( 
'wgUserLanguage' )
+                               } );
+                               eventLogger.logEventQueue.fire();
+                       } );
+               }
+       },
+       /**
+        * Local wrapper for 'mw.eventLog.logEvent' which handles default params
+        * and ensures the correct schema is loaded.
+        *
+        * @param {Object} event Event action and optional fields
+        * @param {int} [timeout] Fail the request if it is not completed within
+        *   the specified timeout. Can be use for example to log actions that
+        *   cause the browser to navigate to other pages.
+        * @return {jQuery.Promise} jQuery Promise object for the logging call
+        */
+       log: function ( event, timeout ) {
+               // 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
+               // and register our timeout if requested (for links).
+               var deferred = $.Deferred();
+
+               this.logEventQueue.add( function () {
+                       mw.eventLog.logEvent( 'UniversalLanguageSelector', 
event )
+                               .done( deferred.resolve )
+                               .fail( deferred.reject );
+               } );
+
+               if ( timeout !== undefined ) {
+                       window.setTimeout( deferred.reject, timeout );
+               }
+
+               return deferred.promise();
+       },
+
+       /**
+        * Listen for event logging
+        */
+       listen: function () {
+               var emitter = mw.uls.eventemitter;
+
+               emitter.once('mw.uls.settings.open', $.proxy( 
this.ulsSettingsOpen, this ) );
+               emitter.on('mw.uls.language.revert', $.proxy( 
this.ulsLanguageRevert, this ) );
+       },
+
+       /**
+        * Log language settings open, only once
+        * @param {Array} args
+        */
+       ulsSettingsOpen: function ( args ) {
+               this.log( {
+                       action: 'settings-open',
+                       context: args
+               } );
+       },
+
+       /**
+        * Log language revert
+        */
+       ulsLanguageRevert: function () {
+               this.log( { action: 'ui-lang-revert' }, 500 );
+       }
+};
+
+mw.uls = mw.uls || {};
+mw.uls.eventlogger= new ULSEventLogger();
+
+}( jQuery, mediaWiki ) );
\ No newline at end of file
diff --git a/resources/js/ext.uls.interface.js 
b/resources/js/ext.uls.interface.js
index dfe3778..999a48e 100644
--- a/resources/js/ext.uls.interface.js
+++ b/resources/js/ext.uls.interface.js
@@ -91,7 +91,7 @@
                                                .click();
                                } );
                        }
-
+                       mw.uls.eventemitter.emit( 'mw.uls.settings.open', 
['uls'] );
                        uls.hide();
                } );
        }
@@ -121,11 +121,7 @@
                                } );
                        }
 
-                       mw.uls.logEvent( {
-                               action: 'settings-open',
-                               context: 'uls'
-                       } );
-
+                       mw.uls.eventemitter.emit( 'mw.uls.settings.open', 
['uls'] );
                        uls.hide();
                } );
        }
@@ -252,10 +248,8 @@
                        // there wont be multiple event handlers bound to same 
click.
                        $( 'a.uls-prevlang-link' ).on( 'click.ulstipsy', 
function ( event ) {
                                event.preventDefault();
-                               mw.uls.logEvent( { action: 'ui-lang-revert' }, 
500 )
-                                       .always( function () {
-                                               mw.uls.changeLanguage( 
event.target.lang );
-                                       } );
+                               mw.uls.eventemitter.emit( 
'mw.uls.language.revert' );
+                               mw.uls.changeLanguage( event.target.lang );
                        } );
                        tipsyTimer = window.setTimeout( function () {
                                hideTipsy();
@@ -333,10 +327,7 @@
 
                                        if ( languagesettings ) {
                                                if ( !languagesettings.shown ) {
-                                                       mw.uls.logEvent( {
-                                                               action: 
'settings-open',
-                                                               context: 
eventParams && eventParams.source || 'interlanguage'
-                                                       } );
+                                                       
mw.uls.eventemitter.emit( 'mw.uls.settings.open', [eventParams && 
eventParams.source || 'interlanguage'] );
                                                }
                                        } else {
                                                // Initialize the Language 
settings window
@@ -385,10 +376,7 @@
 
                                        if ( languagesettings ) {
                                                if ( !languagesettings.shown ) {
-                                                       mw.uls.logEvent( {
-                                                               action: 
'settings-open',
-                                                               context: 
eventParams && eventParams.source || 'personal'
-                                                       } );
+                                                       
mw.uls.eventemitter.emit( 'mw.uls.settings.open', [eventParams && 
eventParams.source || 'personal'] );
                                                }
                                        } else {
                                                mw.loader.using( 
mw.uls.languageSettingsModules, function () {
@@ -407,10 +395,7 @@
 
                                        if ( uls ) {
                                                if ( !uls.shown ) {
-                                                       mw.uls.logEvent( {
-                                                               action: 
'settings-open',
-                                                               context: 
eventParams && eventParams.source || 'personal'
-                                                       } );
+                                                       
mw.uls.eventemitter.emit( 'mw.uls.settings.open', [eventParams && 
eventParams.source || 'personal'] );
                                                }
                                        } else {
                                                // ULS options that are common 
to all modes of showing

-- 
To view, visit https://gerrit.wikimedia.org/r/79047
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I59dfcfb25c1acb85376b56239f2355ee7c4aff1e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UniversalLanguageSelector
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to