r99496 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99495‎ | r99496 | r99497 >
Date:17:02, 11 October 2011
Author:kaldari
Status:ok
Tags:
Comment:
reverting new banner loading scheme for now - reverts r99341 and r99383
Modified paths:
  • /trunk/extensions/CentralNotice/CentralNotice.php (modified) (history)
  • /trunk/extensions/CentralNotice/special/SpecialBannerController.php (modified) (history)
  • /trunk/extensions/CentralNotice/special/SpecialBannerListLoader.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/special/SpecialBannerListLoader.php
@@ -1,7 +1,6 @@
22 <?php
33
44 /**
5 - * Note: This file is deprecated and should be deleted if the new banner loading system works better.
65 * Generates JSON files listing all the banners for a particular site
76 */
87 class SpecialBannerListLoader extends UnlistedSpecialPage {
Index: trunk/extensions/CentralNotice/special/SpecialBannerController.php
@@ -4,8 +4,8 @@
55 * Generates Javascript file which controls banner selection on the client side
66 */
77 class SpecialBannerController extends UnlistedSpecialPage {
8 - protected $sharedMaxAge = 300; // Cache for 5 minutes on the server side
9 - protected $maxAge = 300; // Cache for 5 minutes on the client side
 8+ protected $sharedMaxAge = 3600; // Cache for 1 hour on the server side
 9+ protected $maxAge = 3600; // Cache for 1 hour on the client side
1010
1111 function __construct() {
1212 // Register special page
@@ -19,11 +19,16 @@
2020 $this->sendHeaders();
2121
2222 $content = $this->getOutput();
23 - echo $content;
 23+ if ( strlen( $content ) == 0 ) {
 24+ // Hack for IE/Mac 0-length keepalive problem, see RawPage.php
 25+ echo "/* Empty */";
 26+ } else {
 27+ echo $content;
 28+ }
2429 }
2530
2631 /**
27 - * Generate the HTTP response headers
 32+ * Generate the HTTP response headers for the banner controller
2833 */
2934 function sendHeaders() {
3035 global $wgJsMimeType;
@@ -40,7 +45,7 @@
4146 function getOutput() {
4247 global $wgCentralPagePath, $wgContLang;
4348
44 - $js = $this->getAllBannerLists() . $this->getScriptFunctions() . $this->getToggleScripts();
 49+ $js = $this->getScriptFunctions() . $this->getToggleScripts();
4550 $js .= <<<JAVASCRIPT
4651 ( function( $ ) {
4752 $.ajaxSetup({ cache: true });
@@ -76,12 +81,22 @@
7782 } else {
7883 var geoLocation = Geo.country; // pull the geo info
7984 }
80 - var bannerList = $.parseJSON( wgBannerList[geoLocation] );
81 - $.centralNotice.fn.chooseBanner( bannerList );
 85+ var bannerListQuery = $.param( { 'language': wgContentLanguage, 'project': wgNoticeProject, 'country': geoLocation } );
 86+JAVASCRIPT;
 87+ $js .= "\n\t\t\t\tvar bannerListURL = wgScript + '?title=' + encodeURIComponent('" .
 88+ $wgContLang->specialPage( 'BannerListLoader' ) .
 89+ "') + '&cache=/cn.js&' + bannerListQuery;\n";
 90+ $js .= <<<JAVASCRIPT
 91+ var request = $.ajax( {
 92+ url: bannerListURL,
 93+ dataType: 'json',
 94+ success: $.centralNotice.fn.chooseBanner
 95+ } );
8296 },
8397 'chooseBanner': function( bannerList ) {
8498 // Convert the json object to a true array
8599 bannerList = Array.prototype.slice.call( bannerList );
 100+
86101 // Make sure there are some banners to choose from
87102 if ( bannerList.length == 0 ) return false;
88103
@@ -122,29 +137,17 @@
123138 }
124139 }
125140 }
126 - // Initialize the query string vars
127 - $.centralNotice.fn.getQueryStringVariables();
128 - if ( Geo.country ) {
129 - // We know the user's country so go ahead and load everything
 141+ $( document ).ready( function () {
 142+ // Initialize the query string vars
 143+ $.centralNotice.fn.getQueryStringVariables();
130144 if( $.centralNotice.data.getVars['banner'] ) {
131 - // We're forcing one banner
 145+ // if we're forcing one banner
132146 $.centralNotice.fn.loadBanner( $.centralNotice.data.getVars['banner'] );
133147 } else {
134148 // Look for banners ready to go NOW
135149 $.centralNotice.fn.loadBannerList( $.centralNotice.data.getVars['country'] );
136150 }
137 - } else {
138 - // We don't know the user's country yet, so we have to wait for the GeoIP lookup
139 - $( document ).ready( function () {
140 - if( $.centralNotice.data.getVars['banner'] ) {
141 - // We're forcing one banner
142 - $.centralNotice.fn.loadBanner( $.centralNotice.data.getVars['banner'] );
143 - } else {
144 - // Look for banners ready to go NOW
145 - $.centralNotice.fn.loadBannerList( $.centralNotice.data.getVars['country'] );
146 - }
147 - } ); //document ready
148 - }
 151+ } ); //document ready
149152 } )( jQuery );
150153 JAVASCRIPT;
151154 return $js;
@@ -215,41 +218,5 @@
216219 JAVASCRIPT;
217220 return $script;
218221 }
219 -
220 - /**
221 - * Generate all the banner lists for all the countries
222 - */
223 - function getAllBannerLists() {
224 - $script = "var wgBannerList = new Array();\r\n";
225 - $countriesList = CentralNoticeDB::getCountriesList();
226 - foreach ( $countriesList as $countryCode => $countryName ) {
227 - $script .= "wgBannerList['$countryCode'] = '".$this->getBannerList( $countryCode )."';\r\n";
228 - }
229 - return $script;
230 - }
231 -
232 - /**
233 - * Generate JSON banner list for a given country
234 - */
235 - function getBannerList( $country ) {
236 - global $wgNoticeProject, $wgNoticeLang;
237 - $banners = array();
238 -
239 - // See if we have any preferred campaigns for this language and project
240 - $campaigns = CentralNoticeDB::getCampaigns( $wgNoticeProject, $wgNoticeLang, null, 1, 1, $country );
241 -
242 - // Quick short circuit to show preferred campaigns
243 - if ( $campaigns ) {
244 - // Pull banners
245 - $banners = CentralNoticeDB::getCampaignBanners( $campaigns );
246 - }
247222
248 - // Didn't find any preferred banners so do an old style lookup
249 - if ( !$banners ) {
250 - $banners = CentralNoticeDB::getBannersByTarget( $wgNoticeProject, $wgNoticeLang, $country );
251 - }
252 -
253 - return FormatJson::encode( $banners );
254 - }
255 -
256223 }
Index: trunk/extensions/CentralNotice/CentralNotice.php
@@ -102,8 +102,9 @@
103103
104104 if ( $wgCentralNoticeLoader ) {
105105 $wgHooks['LoadExtensionSchemaUpdates'][] = 'efCentralNoticeSchema';
 106+ $wgHooks['BeforePageDisplay'][] = 'efCentralNoticeLoader';
106107 $wgHooks['MakeGlobalVariablesScript'][] = 'efCentralNoticeDefaults';
107 - $wgHooks['SiteNoticeAfter'][] = 'efCentralNoticeLoader';
 108+ $wgHooks['SiteNoticeAfter'][] = 'efCentralNoticeDisplay';
108109 $wgHooks['SkinAfterBottomScripts'][] = 'efCentralNoticeGeoLoader';
109110 }
110111
@@ -199,38 +200,40 @@
200201 return true;
201202 }
202203
203 -function efCentralNoticeGeoLoader( $skin, &$text ) {
204 - // Insert the geo IP lookup and cookie setter
205 - $text .= Html::linkedScript( "//geoiplookup.wikimedia.org/" );
206 - $cookieScript = <<<JAVASCRIPT
207 -var e = new Date();
208 -e.setTime( e.getTime() + (30*24*60*60*1000) ); // 30 days
209 -document.cookie = 'geoCountry=' + Geo.country + '; expires=' + e.toGMTString() + '; path=/';
 204+function efCentralNoticeLoader( $out, $skin ) {
 205+ global $wgOut;
210206
211 -JAVASCRIPT;
212 - $text .= Html::inlineScript( $cookieScript );
 207+ // Include '.js' to exempt script from squid cache expiration override
 208+ $centralLoader = SpecialPage::getTitleFor( 'BannerController' )->getLocalUrl( 'cache=/cn.js' );
 209+
 210+ // Insert the banner controller Javascript into the <head>
 211+ $wgOut->addScriptFile( $centralLoader );
 212+
213213 return true;
214214 }
215215
 216+function efCentralNoticeGeoLoader( $skin, &$text ) {
 217+ // Insert the geo IP lookup
 218+ $text .= '<script type="text/javascript" src="//geoiplookup.wikimedia.org/"></script>';
 219+ return true;
 220+}
 221+
216222 function efCentralNoticeDefaults( &$vars ) {
217223 global $wgNoticeProject;
218 - // Initialize global Javascript variables
 224+ // Initialize global Javascript variables. We initialize Geo with empty values so if the geo
 225+ // IP lookup fails we don't have any surprises.
219226 $geo = (object)array();
220227 $geo->{'city'} = '';
221 - if ( array_key_exists( 'geoCountry', $_COOKIE ) && $_COOKIE['geoCountry'] != '' ) {
222 - $geo->{'country'} = $_COOKIE['geoCountry'];
223 - } else {
224 - $geo->{'country'} = '';
225 - }
226 - $vars['Geo'] = $geo;
 228+ $geo->{'country'} = '';
 229+ $vars['Geo'] = $geo; // change this to wgGeo as soon as Mark updates on his end
227230 $vars['wgNoticeProject'] = $wgNoticeProject;
228231 return true;
229232 }
230233
231 -function efCentralNoticeLoader( &$notice ) {
 234+function efCentralNoticeDisplay( &$notice ) {
232235 // setup siteNotice div
233 - // Include '.js' to exempt script from squid cache expiration override
234 - $centralLoader = SpecialPage::getTitleFor( 'BannerController' )->getLocalUrl( 'cache=/cn.js' );
235 - $notice .= '<!-- centralNotice loads here --><script type="text/javascript" src="'.$centralLoader.'"></script>';
 236+ $notice =
 237+ '<!-- centralNotice loads here -->'. // hack for IE8 to collapse empty div
 238+ $notice;
236239 return true;
237240 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r100100MFT r92510, r92676, r96496, r97304, r99160, r99165, r99169, r99176, r99178, r...awjrichards23:56, 17 October 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r99341entirely new scheme to optimize banner loading - down from 1.1 seconds to 0.1...kaldari08:33, 9 October 2011
r99383follow-up to r99341 - abstracting script calls, switching to camelcase for co...kaldari23:07, 9 October 2011

Status & tagging log