r73104 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73103‎ | r73104 | r73105 >
Date:01:09, 16 September 2010
Author:kaldari
Status:ok
Tags:
Comment:
syncing banner selection code from newCentralNotice.js
Modified paths:
  • /trunk/extensions/CentralNotice/SpecialBannerController.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/SpecialBannerController.php
@@ -82,24 +82,28 @@
8383 // Make sure there are some banners to choose from
8484 if ( bannerList.length == 0 ) return false;
8585
86 - var groomedBannerList = [];
 86+ var totalWeight = 0;
 87+ // run through the bannerlist and sum the weights of all banners
 88+ for( var i = 0; i < bannerList.length; i++ ) {
 89+ totalWeight += bannerList[i].weight;
 90+ }
8791
 92+ // Select a random integer between 0 and our total weight
 93+ var pointer = Math.floor( Math.random() * totalWeight ),
 94+ selectedBanner = bannerList[0],
 95+ w = 0;
 96+ // Run through the banner list and start accumulating weights
8897 for( var i = 0; i < bannerList.length; i++ ) {
89 - // Only include this banner if it's inteded for the current user
90 - if( ( wgUserName && bannerList[i].display_account ) || ( !wgUserName && bannerList[i].display_anon == 1 ) ) {
91 - // add the banner to our list once per weight
92 - for( var j=0; j < bannerList[i].weight; j++ ) {
93 - groomedBannerList.push( bannerList[i] );
94 - }
 98+ w += bannerList[i].weight;
 99+ // when the weight tally exceeds the random integer, return the banner and stop the loop
 100+ if( w < pointer ) {
 101+ selectedBanner = bannerList[i];
 102+ break;
95103 }
96104 }
97 -
98 - // Return if there's nothing left after the grooming
99 - if( groomedBannerList.length == 0 ) return false;
100 -
101 - // Load a random banner from our groomed list
 105+ // Return our selected banner
102106 $.centralNotice.fn.loadBanner(
103 - groomedBannerList[ Math.floor( Math.random() * groomedBannerList.length ) ].name
 107+ selectedBanner.name
104108 );
105109 },
106110 'displayBanner': function( bannerHTML ) {

Status & tagging log