Index: trunk/extensions/CentralNotice/special/SpecialBannerListLoader.php |
— | — | @@ -1,7 +1,6 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Note: This file is deprecated and should be deleted if the new banner loading system works better. |
6 | 5 | * Generates JSON files listing all the banners for a particular site |
7 | 6 | */ |
8 | 7 | class SpecialBannerListLoader extends UnlistedSpecialPage { |
Index: trunk/extensions/CentralNotice/special/SpecialBannerController.php |
— | — | @@ -4,8 +4,8 @@ |
5 | 5 | * Generates Javascript file which controls banner selection on the client side |
6 | 6 | */ |
7 | 7 | 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 |
10 | 10 | |
11 | 11 | function __construct() { |
12 | 12 | // Register special page |
— | — | @@ -19,11 +19,16 @@ |
20 | 20 | $this->sendHeaders(); |
21 | 21 | |
22 | 22 | $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 | + } |
24 | 29 | } |
25 | 30 | |
26 | 31 | /** |
27 | | - * Generate the HTTP response headers |
| 32 | + * Generate the HTTP response headers for the banner controller |
28 | 33 | */ |
29 | 34 | function sendHeaders() { |
30 | 35 | global $wgJsMimeType; |
— | — | @@ -40,7 +45,7 @@ |
41 | 46 | function getOutput() { |
42 | 47 | global $wgCentralPagePath, $wgContLang; |
43 | 48 | |
44 | | - $js = $this->getAllBannerLists() . $this->getScriptFunctions() . $this->getToggleScripts(); |
| 49 | + $js = $this->getScriptFunctions() . $this->getToggleScripts(); |
45 | 50 | $js .= <<<JAVASCRIPT |
46 | 51 | ( function( $ ) { |
47 | 52 | $.ajaxSetup({ cache: true }); |
— | — | @@ -76,12 +81,22 @@ |
77 | 82 | } else { |
78 | 83 | var geoLocation = Geo.country; // pull the geo info |
79 | 84 | } |
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 | + } ); |
82 | 96 | }, |
83 | 97 | 'chooseBanner': function( bannerList ) { |
84 | 98 | // Convert the json object to a true array |
85 | 99 | bannerList = Array.prototype.slice.call( bannerList ); |
| 100 | + |
86 | 101 | // Make sure there are some banners to choose from |
87 | 102 | if ( bannerList.length == 0 ) return false; |
88 | 103 | |
— | — | @@ -122,29 +137,17 @@ |
123 | 138 | } |
124 | 139 | } |
125 | 140 | } |
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(); |
130 | 144 | if( $.centralNotice.data.getVars['banner'] ) { |
131 | | - // We're forcing one banner |
| 145 | + // if we're forcing one banner |
132 | 146 | $.centralNotice.fn.loadBanner( $.centralNotice.data.getVars['banner'] ); |
133 | 147 | } else { |
134 | 148 | // Look for banners ready to go NOW |
135 | 149 | $.centralNotice.fn.loadBannerList( $.centralNotice.data.getVars['country'] ); |
136 | 150 | } |
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 |
149 | 152 | } )( jQuery ); |
150 | 153 | JAVASCRIPT; |
151 | 154 | return $js; |
— | — | @@ -215,41 +218,5 @@ |
216 | 219 | JAVASCRIPT; |
217 | 220 | return $script; |
218 | 221 | } |
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 | | - } |
247 | 222 | |
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 | | - |
256 | 223 | } |
Index: trunk/extensions/CentralNotice/CentralNotice.php |
— | — | @@ -102,8 +102,9 @@ |
103 | 103 | |
104 | 104 | if ( $wgCentralNoticeLoader ) { |
105 | 105 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'efCentralNoticeSchema'; |
| 106 | + $wgHooks['BeforePageDisplay'][] = 'efCentralNoticeLoader'; |
106 | 107 | $wgHooks['MakeGlobalVariablesScript'][] = 'efCentralNoticeDefaults'; |
107 | | - $wgHooks['SiteNoticeAfter'][] = 'efCentralNoticeLoader'; |
| 108 | + $wgHooks['SiteNoticeAfter'][] = 'efCentralNoticeDisplay'; |
108 | 109 | $wgHooks['SkinAfterBottomScripts'][] = 'efCentralNoticeGeoLoader'; |
109 | 110 | } |
110 | 111 | |
— | — | @@ -199,38 +200,40 @@ |
200 | 201 | return true; |
201 | 202 | } |
202 | 203 | |
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; |
210 | 206 | |
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 | + |
213 | 213 | return true; |
214 | 214 | } |
215 | 215 | |
| 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 | + |
216 | 222 | function efCentralNoticeDefaults( &$vars ) { |
217 | 223 | 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. |
219 | 226 | $geo = (object)array(); |
220 | 227 | $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 |
227 | 230 | $vars['wgNoticeProject'] = $wgNoticeProject; |
228 | 231 | return true; |
229 | 232 | } |
230 | 233 | |
231 | | -function efCentralNoticeLoader( &$notice ) { |
| 234 | +function efCentralNoticeDisplay( &$notice ) { |
232 | 235 | // 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; |
236 | 239 | return true; |
237 | 240 | } |