r72948 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72947‎ | r72948 | r72949 >
Date:00:44, 14 September 2010
Author:kaldari
Status:ok (Comments)
Tags:
Comment:
fix groomedBannerList bug, add in more support for geotargetting, remove obsolete css definitions
Modified paths:
  • /trunk/extensions/CentralNotice/CentralNotice.php (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialBannerController.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/CentralNotice.php
@@ -130,6 +130,9 @@
131131
132132 $centralLoader = SpecialPage::getTitleFor( 'BannerController' )->getLocalUrl();
133133
 134+ // Insert the geo IP lookup into the <head>
 135+ $wgOut->addScriptFile( 'http://geoiplookup.wikimedia.org/' );
 136+
134137 // Insert the banner controller Javascript into the <head>
135138 $wgOut->addScriptFile( $centralLoader );
136139
@@ -138,17 +141,20 @@
139142
140143 function efCentralNoticeDefaults( &$vars ) {
141144 global $wgNoticeProject;
142 - // Initialize global Javascript variables. We initialize wgNotice to empty so if the notice
143 - // script fails we don't have any surprises.
144 - $vars['wgNotice'] = '';
 145+ // Initialize global Javascript variables. We initialize Geo with empty values so if the geo
 146+ // IP lookup fails we don't have any surprises.
 147+ $geo = (object)array();
 148+ $geo->{'city'} = '';
 149+ $geo->{'country'} = '';
 150+ $vars['Geo'] = $geo; // change this to wgGeo as soon as Mark updates on his end
145151 $vars['wgNoticeProject'] = $wgNoticeProject;
146152 return true;
147153 }
148154
149155 function efCentralNoticeDisplay( &$notice ) {
150 - // Slip in loading of the banner (inside the siteNotice div)
 156+ // setup siteNotice div
151157 $notice =
152 - Html::inlineScript( "if (wgNotice != '') document.writeln(wgNotice);" ) .
 158+ '<!-- centralNotice loads here -->'. // hack for IE8 to collapse empty div
153159 $notice;
154160 return true;
155161 }
Index: trunk/extensions/CentralNotice/SpecialBannerController.php
@@ -53,12 +53,11 @@
5454 },
5555 'fn': {
5656 'loadBanner': function( bannerName ) {
57 - // get the requested banner from /centralnotice/banners/<bannername>/<wgUserLanguage>.js
 57+ // Get the requested banner from /centralnotice/banners/<bannername>/<wgUserLanguage>.js
5858 var bannerPage = 'Special:BannerLoader?banner='+bannerName+'&userlang='+wgContentLanguage+'&sitename='+wgNoticeProject;
5959 //centralized version:
6060 //var bannerURL = 'http://meta.wikimedia.org/wiki/'+bannerPage;
61 - //var bannerURL = wgArticlePath.replace( '$1', bannerPage );
62 - var bannerURL = 'http://localhost/~rkaldari/banner.html';
 61+ var bannerURL = wgArticlePath.replace( '$1', bannerPage );
6362 var request = $.ajax( {
6463 url: bannerURL,
6564 dataType: 'html',
@@ -69,16 +68,11 @@
7069 },
7170 'loadBannerList': function( timestamp ) {
7271 var listURL;
73 - if ( timestamp ) {
74 - listURL = "TBD"
75 - } else {
76 - // http://geoiplookup.wikimedia.org/
77 - var geoLocation = 'US'; // Hard-coding for now
78 - var bannerListPage = 'Special:BannerListLoader?language='+wgContentLanguage+'&project='+wgNoticeProject+'&location='+geoLocation;
79 - //centralized version:
80 - //var bannerListURL = 'http://meta.wikimedia.org/wiki/'+bannerListPage;
81 - var bannerListURL = wgArticlePath.replace( '$1', bannerListPage );
82 - }
 72+ var geoLocation = Geo.country; // pull the geo info
 73+ var bannerListPage = 'Special:BannerListLoader?language='+wgContentLanguage+'&project='+wgNoticeProject+'&location='+geoLocation;
 74+ //centralized version:
 75+ //var bannerListURL = 'http://meta.wikimedia.org/wiki/'+bannerListPage;
 76+ var bannerListURL = wgArticlePath.replace( '$1', bannerListPage );
8377 var request = $.ajax( {
8478 url: bannerListURL,
8579 dataType: 'json',
@@ -86,7 +80,7 @@
8781 } );
8882 },
8983 'chooseBanner': function( bannerList ) {
90 - // convert the json object to a true array
 84+ // Convert the json object to a true array
9185 bannerList = Array.prototype.slice.call( bannerList );
9286
9387 // Make sure there are some banners to choose from
@@ -95,8 +89,8 @@
9690 var groomedBannerList = [];
9791
9892 for( var i = 0; i < bannerList.length; i++ ) {
99 - // only include this banner if it's inteded for the current user
100 - if( ( wgUserName ? bannerList[i].display_account == 1 : bannerList.display_anon == 1 ) ) {
 93+ // Only include this banner if it's inteded for the current user
 94+ if( ( wgUserName && bannerList[i].display_account ) || ( !wgUserName && bannerList[i].display_anon == 1 ) ) {
10195 // add the banner to our list once per weight
10296 for( var j=0; j < bannerList[i].weight; j++ ) {
10397 groomedBannerList.push( bannerList[i] );
@@ -104,18 +98,18 @@
10599 }
106100 }
107101
108 - // return if there's nothing left after the grooming
 102+ // Return if there's nothing left after the grooming
109103 if( groomedBannerList.length == 0 ) return false;
110 - // load a random banner from our groomed list
111104
 105+ // Load a random banner from our groomed list
112106 $.centralNotice.fn.loadBanner(
113107 groomedBannerList[ Math.floor( Math.random() * groomedBannerList.length ) ].name
114 - );
 108+ );
115109 },
116110 'displayBanner': function( bannerHTML ) {
117 - // inject the banner html into the page
 111+ // Inject the banner html into the page
118112 $( '#siteNotice' )
119 - .prepend( '<div id="centralnotice" class="' + ( wgNoticeToggleState ? 'expanded' : 'collapsed' ) + '">' + bannerHTML + '</div>' );
 113+ .prepend( '<div id="centralNotice" class="' + ( wgNoticeToggleState ? 'expanded' : 'collapsed' ) + '">' + bannerHTML + '</div>' );
120114 },
121115 'getQueryStringVariables': function() {
122116 document.location.search.replace( /\??(?:([^=]+)=([^&]*)&?)/g, function () {
@@ -128,18 +122,10 @@
129123 }
130124 }
131125 $( document ).ready( function () {
132 - // initialize the query string vars
 126+ // Initialize the query string vars
133127 $.centralNotice.fn.getQueryStringVariables();
134 - if( $.centralNotice.data.getVars['forceBanner'] ) {
135 - // if we're forcing one banner
136 - $.centralNotice.fn.loadBanner( $.centralNotice.data.getVars['forceBanner'] );
137 - } else if ( $.centralNotice.data.getVars['forceTimestamp'] ) {
138 - // if we're forcing a future campaign time
139 - $.centralNotice.fn.loadBannerList( $.centralNotice.data.getVars['forceTimestamp'] );
140 - } else {
141 - // look for banners ready to go NOW
142 - $.centralNotice.fn.loadBannerList( );
143 - }
 128+ // Look for banners ready to go NOW
 129+ $.centralNotice.fn.loadBannerList( );
144130 } ); //document ready
145131 } )( jQuery );
146132 EOT;
@@ -151,14 +137,8 @@
152138 $showStyle = <<<END
153139 <style type="text/css">
154140 #centralNotice .siteNoticeSmall {display:none;}
155 -#centralNotice .siteNoticeSmallAnon {display:none;}
156 -#centralNotice .siteNoticeSmallUser {display:none;}
157141 #centralNotice.collapsed .siteNoticeBig {display:none;}
158142 #centralNotice.collapsed .siteNoticeSmall {display:block;}
159 -#centralNotice.collapsed .siteNoticeSmallUser {display:block;}
160 -#centralNotice.collapsed .siteNoticeSmallAnon {display:block;}
161 -#centralNotice.anonnotice .siteNoticeSmallUser {display:none !important;}
162 -#centralNotice.usernotice .siteNoticeSmallAnon {display:none !important;}
163143 </style>
164144 END;
165145 $encShowStyle = Xml::encodeJsVar( $showStyle );

Follow-up revisions

RevisionCommit summaryAuthorDate
r105015more logical setting for geokaldari22:28, 2 December 2011

Comments

#Comment by Catrope (talk | contribs)   18:47, 14 September 2010
+	$geo = (object)array();
+	$geo->{'city'} = '';
+	$geo->{'country'} = '';

Simpler: $geo = (object)array( 'city' => , 'country' => );

#Comment by Catrope (talk | contribs)   18:48, 14 September 2010

Ah, (object)array( 'city' => '', 'country' => '' );

#Comment by Krinkle (talk | contribs)   20:49, 2 December 2011

Why (object)array(); ?

Associative arrays in PHP are what are objects in JavaScript (json_encode maps it). Actual PHP objects don't have a JSON equivalent afaik, when forced to become JSON, PHP probably converts it to an array with only the public variables, thus losing the other Object data (such as class name, parent, private properties, any methods etc).

#Comment by Kaldari (talk | contribs)   22:31, 2 December 2011

Your right. Fixed in r105015.

Status & tagging log