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

Revision: 72759
Author:   adam
Date:     2010-09-10 21:35:35 +0000 (Fri, 10 Sep 2010)

Log Message:
-----------
Implimenting weighted banner picking

Modified Paths:
--------------
    trunk/extensions/CentralNotice/newCentralNotice.js

Modified: trunk/extensions/CentralNotice/newCentralNotice.js
===================================================================
--- trunk/extensions/CentralNotice/newCentralNotice.js  2010-09-10 21:27:19 UTC 
(rev 72758)
+++ trunk/extensions/CentralNotice/newCentralNotice.js  2010-09-10 21:35:35 UTC 
(rev 72759)
@@ -36,6 +36,7 @@
                                if ( timestamp ) {
                                        listURL = "TBD"
                                } else {
+                                       // http://geoiplookup.wikimedia.org/
                                        var geoLocation = 'US'; // Hard-coding 
for now
                                        var bannerListPage = 
'Special:BannerListLoader?language='+wgContentLanguage+'&project='+wgNoticeProject+'&location='+geoLocation;
                                        //centralized version:
@@ -45,19 +46,40 @@
                                var request = $.ajax( {
                                        url: bannerListURL,
                                        dataType: 'json',
-                                       success: function( data ) {
-                                               
$.centralNotice.fn.chooseBanner( data );
-                                       }
+                                       success: $.centralNotice.fn.chooseBanner
                                } );
                        },
                        'chooseBanner': function( bannerList ) {
-                               // pick a banner based on logged-in status and 
geotargetting
-                               var bannerHTML = bannerList[0].html;
-                               $.centralNotice.fn.displayBanner( bannerHTML );
+                               // convert the json object to a true array
+                               bannerList = Array.prototype.slice.call( 
bannerList );
+                               
+                               // Make sure there are some banners to choose 
from
+                               if ( bannerList.length == 0 ) return false;
+                               
+                               var groomedBannerList = [];
+                               
+                               for( var i = 0; i < bannerList.length; i++ ) {
+                                       // only include this banner if it's 
inteded for the current user
+                                       if( ( wgUserName ? 
bannerList[i].display_account == 1 : bannerList.display_anon == 1 ) ) {
+                                               // add the banner to our list 
once per weight
+                                               for( var j=0; j < 
bannerList[i].weight; j++ ) {
+                                                       groomedBannerList.push( 
bannerList[i] );
+                                               }
+                                       }
+                               }
+                               
+                               // return if there's nothing left after the 
grooming
+                               if( groomedBannerList.length == 0 ) return 
false;
+                               // load a random banner from our groomed list
+                               
+                               $.centralNotice.fn.loadBanner( 
+                                       groomedBannerList[ Math.floor( 
Math.random() * groomedBannerList.length ) ].name
+                                );
                        },
                        'displayBanner': function( bannerHTML ) {
                                // inject the banner html into the page
-                               $( '#siteNotice' ).prepend('<div 
id="centralnotice" class="'+(wgNoticeToggleState ? 'expanded' : 
'collapsed')+'">'+bannerHTML+'</div>');
+                               $( '#siteNotice' )
+                                       .prepend( '<div id="centralnotice" 
class="' + ( wgNoticeToggleState ? 'expanded' : 'collapsed' ) + '">' + 
bannerHTML + '</div>' );
                        },
                        'getQueryStringVariables': function() {
                                document.location.search.replace( 
/\??(?:([^=]+)=([^&]*)&?)/g, function () {



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

Reply via email to