http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88254

Revision: 88254
Author:   krinkle
Date:     2011-05-16 17:58:54 +0000 (Mon, 16 May 2011)
Log Message:
-----------
Split mw.user from mediawiki.js into its own module
* Split mw.user from mediawiki.js into its own module
* Except for "new Map()" to "new mw.Map()", no code was changed.
* Merged addModules call in OutputPage->addDefaultModules for 
"mediawiki.action.view.tablesorting" into existing call to addModules (no need 
for two calls, addModules takes an array)
* This also makes the mw.loader.load call at the bottom of mediawiki.js 
redundant (now in dependancies for mediawiki.user in Resources.php)
* Added QUnit test suite for mw.user

Modified Paths:
--------------
    trunk/phase3/includes/OutputPage.php
    trunk/phase3/resources/Resources.php
    trunk/phase3/resources/mediawiki/mediawiki.js
    trunk/phase3/resources/test/index.html
    trunk/phase3/resources/test/unit/mediawiki/mediawiki.js

Added Paths:
-----------
    trunk/phase3/resources/mediawiki/mediawiki.user.js
    trunk/phase3/resources/test/unit/mediawiki/mediawiki.user.js

Modified: trunk/phase3/includes/OutputPage.php
===================================================================
--- trunk/phase3/includes/OutputPage.php        2011-05-16 17:52:47 UTC (rev 
88253)
+++ trunk/phase3/includes/OutputPage.php        2011-05-16 17:58:54 UTC (rev 
88254)
@@ -2335,8 +2335,8 @@
                        $wgUseAjax, $wgAjaxWatch, $wgEnableMWSuggest;
 
                // Add base resources
-               $this->addModules( 'mediawiki.util' );
-               if( $wgIncludeLegacyJavaScript ){
+               $this->addModules( array( 'mediawiki.user', 'mediawiki.util', 
'mediawiki.action.view.tablesorting' ) );
+               if ( $wgIncludeLegacyJavaScript ){
                        $this->addModules( 'mediawiki.legacy.wikibits' );
                }
 
@@ -2358,7 +2358,6 @@
                if( $this->getUser()->getBoolOption( 'editsectiononrightclick' 
) ) {
                        $this->addModules( 
'mediawiki.action.view.rightClickEdit' );
                }
-               $this->addModules( 'mediawiki.action.view.tablesorting' );
        }
 
        /**

Modified: trunk/phase3/resources/Resources.php
===================================================================
--- trunk/phase3/resources/Resources.php        2011-05-16 17:52:47 UTC (rev 
88253)
+++ trunk/phase3/resources/Resources.php        2011-05-16 17:58:54 UTC (rev 
88254)
@@ -434,6 +434,12 @@
        'mediawiki.htmlform' => array(
                'scripts' => 'resources/mediawiki/mediawiki.htmlform.js',
        ),
+       'mediawiki.user' => array(
+               'scripts' => 'resources/mediawiki/mediawiki.user.js',
+               'dependencies' => array(
+                       'jquery.cookie',
+               ),
+       ),
        'mediawiki.util' => array(
                'scripts' => 'resources/mediawiki.util/mediawiki.util.js',
                'dependencies' => array(

Modified: trunk/phase3/resources/mediawiki/mediawiki.js
===================================================================
--- trunk/phase3/resources/mediawiki/mediawiki.js       2011-05-16 17:52:47 UTC 
(rev 88253)
+++ trunk/phase3/resources/mediawiki/mediawiki.js       2011-05-16 17:58:54 UTC 
(rev 88254)
@@ -306,184 +306,6 @@
                return this.map.exists( this.key );
        };
 
-       /**
-        * User object
-        */
-       function User() {
-
-               /* Private Members */
-
-               var that = this;
-
-               /* Public Members */
-
-               this.options = new Map();
-
-               /* Public Methods */
-
-               /**
-                * Generates a random user session ID (32 alpha-numeric 
characters).
-                *
-                * This information would potentially be stored in a cookie to 
identify a user during a
-                * session or series of sessions. It's uniqueness should not be 
depended on.
-                *
-                * @return string random set of 32 alpha-numeric characters
-                */
-               function generateId() {
-                       var id = '';
-                       var seed = 
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
-                       for ( var i = 0, r; i < 32; i++ ) {
-                               r = Math.floor( Math.random() * seed.length );
-                               id += seed.substring( r, r + 1 );
-                       }
-                       return id;
-               }
-
-               /**
-                * Gets the current user's name.
-                *
-                * @return mixed user name string or null if users is anonymous
-                */
-               this.name = function() {
-                       return mw.config.get( 'wgUserName' );
-               };
-
-               /**
-                * Checks if the current user is anonymous.
-                *
-                * @return boolean
-                */
-               this.anonymous = function() {
-                       return that.name() ? false : true;
-               };
-
-               /**
-                * Gets a random session ID automatically generated and kept in 
a cookie.
-                *
-                * This ID is ephemeral for everyone, staying in their browser 
only until they close
-                * their browser.
-                *
-                * Do not use this method before the first call to 
mw.loader.go(), it depends on
-                * jquery.cookie, which is added to the first pay-load just 
after mediaWiki is defined, but
-                * won't be loaded until the first call to go().
-                *
-                * @return string user name or random session ID
-                */
-               this.sessionId = function () {
-                       var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
-                       if ( typeof sessionId == 'undefined' || sessionId === 
null ) {
-                               sessionId = generateId();
-                               $.cookie( 'mediaWiki.user.sessionId', 
sessionId, { 'expires': null, 'path': '/' } );
-                       }
-                       return sessionId;
-               };
-
-               /**
-                * Gets the current user's name or a random ID automatically 
generated and kept in a cookie.
-                *
-                * This ID is persistent for anonymous users, staying in their 
browser up to 1 year. The
-                * expiration time is reset each time the ID is queried, so in 
most cases this ID will
-                * persist until the browser's cookies are cleared or the user 
doesn't visit for 1 year.
-                *
-                * Do not use this method before the first call to 
mw.loader.go(), it depends on
-                * jquery.cookie, which is added to the first pay-load just 
after mediaWiki is defined, but
-                * won't be loaded until the first call to go().
-                *
-                * @return string user name or random session ID
-                */
-               this.id = function() {
-                       var name = that.name();
-                       if ( name ) {
-                               return name;
-                       }
-                       var id = $.cookie( 'mediaWiki.user.id' );
-                       if ( typeof id == 'undefined' || id === null ) {
-                               id = generateId();
-                       }
-                       // Set cookie if not set, or renew it if already set
-                       $.cookie( 'mediaWiki.user.id', id, { 'expires': 365, 
'path': '/' } );
-                       return id;
-               };
-
-               /**
-                * Gets the user's bucket, placing them in one at random based 
on set odds if needed.
-                *
-                * @param key String: Name of bucket
-                * @param options Object: Bucket configuration options
-                * @param options.buckets Object: List of 
bucket-name/relative-probability pairs (required,
-                * must have at least one pair)
-                * @param options.version Number: Version of bucket test, 
changing this forces rebucketing
-                * (optional, default: 0)
-                * @param options.tracked Boolean: Track the event of bucketing 
through the API module of
-                * the ClickTracking extension (optional, default: false)
-                * @param options.expires Number: Length of time (in days) 
until the user gets rebucketed
-                * (optional, default: 30)
-                * @return String: Bucket name - the randomly chosen key of the 
options.buckets object
-                *
-                * @example
-                *     mw.user.bucket( 'test', {
-                *         'buckets': { 'ignored': 50, 'control': 25, 'test': 
25 },
-                *         'version': 1,
-                *         'tracked': true,
-                *         'expires': 7
-                *     } );
-                */
-               this.bucket = function( key, options ) {
-                       options = $.extend( {
-                               'buckets': {},
-                               'version': 0,
-                               'tracked': false,
-                               'expires': 30
-                       }, options || {} );
-                       var cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
-                       var bucket = null;
-                       var version = 0;
-                       // Bucket information is stored as 2 integers, together 
as version:bucket like: "1:2"
-                       if ( typeof cookie === 'string' && cookie.length > 2 && 
cookie.indexOf( ':' ) > 0 ) {
-                               var parts = cookie.split( ':' );
-                               if ( parts.length > 1 && parts[0] == 
options.version ) {
-                                       version = Number( parts[0] );
-                                       bucket = String( parts[1] );
-                               }
-                       }
-                       if ( bucket === null ) {
-                               if ( !$.isPlainObject( options.buckets ) ) {
-                                       throw 'Invalid buckets error. Object 
expected for options.buckets.';
-                               }
-                               version = Number( options.version );
-                               // Find range
-                               var     range = 0, k;
-                               for ( k in options.buckets ) {
-                                       range += options.buckets[k];
-                               }
-                               // Select random value within range
-                               var rand = Math.random() * range;
-                               // Determine which bucket the value landed in
-                               var total = 0;
-                               for ( k in options.buckets ) {
-                                       bucket = k;
-                                       total += options.buckets[k];
-                                       if ( total >= rand ) {
-                                               break;
-                                       }
-                               }
-                               if ( options.tracked ) {
-                                       mw.loader.using( 
'jquery.clickTracking', function() {
-                                               $.trackAction(
-                                                       
'mediaWiki.user.bucket:' + key + '@' + version + ':' + bucket
-                                               );
-                                       } );
-                               }
-                               $.cookie(
-                                       'mediaWiki.user.bucket:' + key,
-                                       version + ':' + bucket,
-                                       { 'path': '/', 'expires': Number( 
options.expires ) }
-                               );
-                       }
-                       return bucket;
-               };
-       }
-
        /* Public Members */
 
        /*
@@ -507,11 +329,6 @@
        this.config = null;
 
        /*
-        * Information about the current user
-        */
-       this.user = new User();
-
-       /*
         * Localization system
         */
        this.messages = new this.Map();
@@ -1434,6 +1251,3 @@
        startUp();
        delete startUp;
 }
-
-// Add jQuery Cookie to initial payload (used in mw.user)
-mw.loader.load( 'jquery.cookie' );

Added: trunk/phase3/resources/mediawiki/mediawiki.user.js
===================================================================
--- trunk/phase3/resources/mediawiki/mediawiki.user.js                          
(rev 0)
+++ trunk/phase3/resources/mediawiki/mediawiki.user.js  2011-05-16 17:58:54 UTC 
(rev 88254)
@@ -0,0 +1,187 @@
+/*
+ * Implementation for mediaWiki.log stub
+ */
+
+(function( $ ) {
+
+       /**
+        * User object
+        */
+       function User() {
+
+               /* Private Members */
+
+               var that = this;
+
+               /* Public Members */
+
+               this.options = new mw.Map();
+
+               /* Public Methods */
+
+               /**
+                * Generates a random user session ID (32 alpha-numeric 
characters).
+                *
+                * This information would potentially be stored in a cookie to 
identify a user during a
+                * session or series of sessions. It's uniqueness should not be 
depended on.
+                *
+                * @return string random set of 32 alpha-numeric characters
+                */
+               function generateId() {
+                       var id = '';
+                       var seed = 
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
+                       for ( var i = 0, r; i < 32; i++ ) {
+                               r = Math.floor( Math.random() * seed.length );
+                               id += seed.substring( r, r + 1 );
+                       }
+                       return id;
+               }
+
+               /**
+                * Gets the current user's name.
+                *
+                * @return mixed user name string or null if users is anonymous
+                */
+               this.name = function() {
+                       return mw.config.get( 'wgUserName' );
+               };
+
+               /**
+                * Checks if the current user is anonymous.
+                *
+                * @return boolean
+                */
+               this.anonymous = function() {
+                       return that.name() ? false : true;
+               };
+
+               /**
+                * Gets a random session ID automatically generated and kept in 
a cookie.
+                *
+                * This ID is ephemeral for everyone, staying in their browser 
only until they close
+                * their browser.
+                *
+                * Do not use this method before the first call to 
mw.loader.go(), it depends on
+                * jquery.cookie, which is added to the first pay-load just 
after mediaWiki is defined, but
+                * won't be loaded until the first call to go().
+                *
+                * @return string user name or random session ID
+                */
+               this.sessionId = function () {
+                       var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
+                       if ( typeof sessionId == 'undefined' || sessionId === 
null ) {
+                               sessionId = generateId();
+                               $.cookie( 'mediaWiki.user.sessionId', 
sessionId, { 'expires': null, 'path': '/' } );
+                       }
+                       return sessionId;
+               };
+
+               /**
+                * Gets the current user's name or a random ID automatically 
generated and kept in a cookie.
+                *
+                * This ID is persistent for anonymous users, staying in their 
browser up to 1 year. The
+                * expiration time is reset each time the ID is queried, so in 
most cases this ID will
+                * persist until the browser's cookies are cleared or the user 
doesn't visit for 1 year.
+                *
+                * Do not use this method before the first call to 
mw.loader.go(), it depends on
+                * jquery.cookie, which is added to the first pay-load just 
after mediaWiki is defined, but
+                * won't be loaded until the first call to go().
+                *
+                * @return string user name or random session ID
+                */
+               this.id = function() {
+                       var name = that.name();
+                       if ( name ) {
+                               return name;
+                       }
+                       var id = $.cookie( 'mediaWiki.user.id' );
+                       if ( typeof id == 'undefined' || id === null ) {
+                               id = generateId();
+                       }
+                       // Set cookie if not set, or renew it if already set
+                       $.cookie( 'mediaWiki.user.id', id, { 'expires': 365, 
'path': '/' } );
+                       return id;
+               };
+
+               /**
+                * Gets the user's bucket, placing them in one at random based 
on set odds if needed.
+                *
+                * @param key String: Name of bucket
+                * @param options Object: Bucket configuration options
+                * @param options.buckets Object: List of 
bucket-name/relative-probability pairs (required,
+                * must have at least one pair)
+                * @param options.version Number: Version of bucket test, 
changing this forces rebucketing
+                * (optional, default: 0)
+                * @param options.tracked Boolean: Track the event of bucketing 
through the API module of
+                * the ClickTracking extension (optional, default: false)
+                * @param options.expires Number: Length of time (in days) 
until the user gets rebucketed
+                * (optional, default: 30)
+                * @return String: Bucket name - the randomly chosen key of the 
options.buckets object
+                *
+                * @example
+                *     mw.user.bucket( 'test', {
+                *         'buckets': { 'ignored': 50, 'control': 25, 'test': 
25 },
+                *         'version': 1,
+                *         'tracked': true,
+                *         'expires': 7
+                *     } );
+                */
+               this.bucket = function( key, options ) {
+                       options = $.extend( {
+                               'buckets': {},
+                               'version': 0,
+                               'tracked': false,
+                               'expires': 30
+                       }, options || {} );
+                       var cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
+                       var bucket = null;
+                       var version = 0;
+                       // Bucket information is stored as 2 integers, together 
as version:bucket like: "1:2"
+                       if ( typeof cookie === 'string' && cookie.length > 2 && 
cookie.indexOf( ':' ) > 0 ) {
+                               var parts = cookie.split( ':' );
+                               if ( parts.length > 1 && parts[0] == 
options.version ) {
+                                       version = Number( parts[0] );
+                                       bucket = String( parts[1] );
+                               }
+                       }
+                       if ( bucket === null ) {
+                               if ( !$.isPlainObject( options.buckets ) ) {
+                                       throw 'Invalid buckets error. Object 
expected for options.buckets.';
+                               }
+                               version = Number( options.version );
+                               // Find range
+                               var     range = 0, k;
+                               for ( k in options.buckets ) {
+                                       range += options.buckets[k];
+                               }
+                               // Select random value within range
+                               var rand = Math.random() * range;
+                               // Determine which bucket the value landed in
+                               var total = 0;
+                               for ( k in options.buckets ) {
+                                       bucket = k;
+                                       total += options.buckets[k];
+                                       if ( total >= rand ) {
+                                               break;
+                                       }
+                               }
+                               if ( options.tracked ) {
+                                       mw.loader.using( 
'jquery.clickTracking', function() {
+                                               $.trackAction(
+                                                       
'mediaWiki.user.bucket:' + key + '@' + version + ':' + bucket
+                                               );
+                                       } );
+                               }
+                               $.cookie(
+                                       'mediaWiki.user.bucket:' + key,
+                                       version + ':' + bucket,
+                                       { 'path': '/', 'expires': Number( 
options.expires ) }
+                               );
+                       }
+                       return bucket;
+               };
+       }
+
+       mw.user = new User();
+
+})(jQuery);
\ No newline at end of file


Property changes on: trunk/phase3/resources/mediawiki/mediawiki.user.js
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/phase3/resources/test/index.html
===================================================================
--- trunk/phase3/resources/test/index.html      2011-05-16 17:52:47 UTC (rev 
88253)
+++ trunk/phase3/resources/test/index.html      2011-05-16 17:58:54 UTC (rev 
88254)
@@ -11,6 +11,7 @@
        <script> function startUp(){} </script>
        <script src="../mediawiki/mediawiki.js"></script>
        <script> mw.config = new mw.Map( false ); </script>
+       <script src="../mediawiki/mediawiki.user.js"></script>
 
        <script src="../jquery/jquery.checkboxShiftClick.js"></script>
        <script src="../jquery/jquery.client.js"></script>
@@ -31,6 +32,7 @@
 
        <!-- Your test suites go here -->
        <script src="unit/mediawiki/mediawiki.js"></script>
+       <script src="unit/mediawiki/mediawiki.user.js"></script>
        <script src="unit/mediawiki.util/mediawiki.util.js"></script>
        <script src="unit/jquery/jquery.colorUtil.js"></script>
 

Modified: trunk/phase3/resources/test/unit/mediawiki/mediawiki.js
===================================================================
--- trunk/phase3/resources/test/unit/mediawiki/mediawiki.js     2011-05-16 
17:52:47 UTC (rev 88253)
+++ trunk/phase3/resources/test/unit/mediawiki/mediawiki.js     2011-05-16 
17:58:54 UTC (rev 88254)
@@ -109,24 +109,6 @@
 // params, toString, parse, plain, escaped, exists
 });
 
-test( 'mw.user', function(){
-
-       ok( mw.user, 'user defined' );
-       ok( mw.user.options instanceof mw.Map, 'user.options instance of 
mw.Map' );
-
-       same( mw.user.name(), null, 'user.name() Anonymous' );
-       ok( mw.user.anonymous(), 'user.anonymous() Anonymous' );
-
-       // Not part of startUp module
-       mw.config.set( 'wgUserName', 'John' );
-
-       equals( mw.user.name(), 'John', 'user.name() Logged-in' );
-       ok( !mw.user.anonymous(), 'user.anonymous() Logged-in' );
-
-       equals( mw.user.id(), 'John', 'user.id() Logged-in' );
-
-});
-
 test( 'mw.loader', function(){
        expect(2);
        

Added: trunk/phase3/resources/test/unit/mediawiki/mediawiki.user.js
===================================================================
--- trunk/phase3/resources/test/unit/mediawiki/mediawiki.user.js                
                (rev 0)
+++ trunk/phase3/resources/test/unit/mediawiki/mediawiki.user.js        
2011-05-16 17:58:54 UTC (rev 88254)
@@ -0,0 +1,30 @@
+module( 'mediawiki.user.js' );
+
+test( '-- Initial check', function(){
+
+       ok( mw.user, 'mw.user defined' );
+
+});
+
+
+test( 'options', function(){
+
+       ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' );
+
+});
+
+test( 'User login status', function(){
+
+       deepEqual( mw.user.name(), null, 'user.name() When anonymous' );
+       ok( mw.user.anonymous(), 'user.anonymous() When anonymous' );
+
+       // Not part of startUp module
+       mw.config.set( 'wgUserName', 'John' );
+
+       equal( mw.user.name(), 'John', 'user.name() When logged-in as John' );
+       ok( !mw.user.anonymous(), 'user.anonymous() When logged-in' );
+
+       equal( mw.user.id(), 'John', 'user.id() When logged-in as John' );
+
+
+});
\ No newline at end of file


Property changes on: 
trunk/phase3/resources/test/unit/mediawiki/mediawiki.user.js
___________________________________________________________________
Added: svn:eol-style
   + native


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

Reply via email to