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

Revision: 99496
Author:   kaldari
Date:     2011-10-11 17:02:33 +0000 (Tue, 11 Oct 2011)
Log Message:
-----------
reverting new banner loading scheme for now - reverts r99341 and r99383

Modified Paths:
--------------
    trunk/extensions/CentralNotice/CentralNotice.php
    trunk/extensions/CentralNotice/special/SpecialBannerController.php
    trunk/extensions/CentralNotice/special/SpecialBannerListLoader.php

Modified: trunk/extensions/CentralNotice/CentralNotice.php
===================================================================
--- trunk/extensions/CentralNotice/CentralNotice.php    2011-10-11 16:55:07 UTC 
(rev 99495)
+++ trunk/extensions/CentralNotice/CentralNotice.php    2011-10-11 17:02:33 UTC 
(rev 99496)
@@ -102,8 +102,9 @@
 
        if ( $wgCentralNoticeLoader ) {
                $wgHooks['LoadExtensionSchemaUpdates'][] = 
'efCentralNoticeSchema';
+               $wgHooks['BeforePageDisplay'][] = 'efCentralNoticeLoader';
                $wgHooks['MakeGlobalVariablesScript'][] = 
'efCentralNoticeDefaults';
-               $wgHooks['SiteNoticeAfter'][] = 'efCentralNoticeLoader';
+               $wgHooks['SiteNoticeAfter'][] = 'efCentralNoticeDisplay';
                $wgHooks['SkinAfterBottomScripts'][] = 
'efCentralNoticeGeoLoader';
        }
 
@@ -199,38 +200,40 @@
        return true;
 }
 
-function efCentralNoticeGeoLoader( $skin, &$text ) {
-       // Insert the geo IP lookup and cookie setter
-       $text .= Html::linkedScript( "//geoiplookup.wikimedia.org/" );
-       $cookieScript = <<<JAVASCRIPT
-var e = new Date();
-e.setTime( e.getTime() + (30*24*60*60*1000) ); // 30 days
-document.cookie = 'geoCountry=' + Geo.country + '; expires=' + e.toGMTString() 
+ '; path=/';
+function efCentralNoticeLoader( $out, $skin ) {
+       global $wgOut;
 
-JAVASCRIPT;
-       $text .= Html::inlineScript( $cookieScript );
+       // Include '.js' to exempt script from squid cache expiration override
+       $centralLoader = SpecialPage::getTitleFor( 'BannerController' 
)->getLocalUrl( 'cache=/cn.js' );
+
+       // Insert the banner controller Javascript into the <head>
+       $wgOut->addScriptFile( $centralLoader );
+
        return true;
 }
 
+function efCentralNoticeGeoLoader( $skin, &$text ) {
+       // Insert the geo IP lookup
+       $text .= '<script type="text/javascript" 
src="//geoiplookup.wikimedia.org/"></script>';
+       return true;
+}
+
 function efCentralNoticeDefaults( &$vars ) {
        global $wgNoticeProject;
-       // Initialize global Javascript variables
+       // Initialize global Javascript variables. We initialize Geo with empty 
values so if the geo
+       // IP lookup fails we don't have any surprises.
        $geo = (object)array();
        $geo->{'city'} = '';
-       if ( array_key_exists( 'geoCountry', $_COOKIE ) && 
$_COOKIE['geoCountry'] != '' ) {
-               $geo->{'country'} = $_COOKIE['geoCountry'];
-       } else {
-               $geo->{'country'} = '';
-       }
-       $vars['Geo'] = $geo;
+       $geo->{'country'} = '';
+       $vars['Geo'] = $geo; // change this to wgGeo as soon as Mark updates on 
his end
        $vars['wgNoticeProject'] = $wgNoticeProject;
        return true;
 }
 
-function efCentralNoticeLoader( &$notice ) {
+function efCentralNoticeDisplay( &$notice ) {
        // setup siteNotice div
-       // Include '.js' to exempt script from squid cache expiration override
-       $centralLoader = SpecialPage::getTitleFor( 'BannerController' 
)->getLocalUrl( 'cache=/cn.js' );
-       $notice .= '<!-- centralNotice loads here --><script 
type="text/javascript" src="'.$centralLoader.'"></script>';
+       $notice =
+               '<!-- centralNotice loads here -->'. // hack for IE8 to 
collapse empty div
+               $notice;
        return true;
 }

Modified: trunk/extensions/CentralNotice/special/SpecialBannerController.php
===================================================================
--- trunk/extensions/CentralNotice/special/SpecialBannerController.php  
2011-10-11 16:55:07 UTC (rev 99495)
+++ trunk/extensions/CentralNotice/special/SpecialBannerController.php  
2011-10-11 17:02:33 UTC (rev 99496)
@@ -4,8 +4,8 @@
  * Generates Javascript file which controls banner selection on the client side
  */
 class SpecialBannerController extends UnlistedSpecialPage {
-       protected $sharedMaxAge = 300; // Cache for 5 minutes on the server side
-       protected $maxAge = 300; // Cache for 5 minutes on the client side
+       protected $sharedMaxAge = 3600; // Cache for 1 hour on the server side
+       protected $maxAge = 3600; // Cache for 1 hour on the client side
 
        function __construct() {
                // Register special page
@@ -19,11 +19,16 @@
                $this->sendHeaders();
                
                $content = $this->getOutput();
-               echo $content;
+               if ( strlen( $content ) == 0 ) {
+                       // Hack for IE/Mac 0-length keepalive problem, see 
RawPage.php
+                       echo "/* Empty */";
+               } else {
+                       echo $content;
+               }
        }
        
        /**
-        * Generate the HTTP response headers
+        * Generate the HTTP response headers for the banner controller
         */
        function sendHeaders() {
                global $wgJsMimeType;
@@ -40,7 +45,7 @@
        function getOutput() {
                global $wgCentralPagePath, $wgContLang;
                
-               $js = $this->getAllBannerLists() . $this->getScriptFunctions() 
. $this->getToggleScripts();
+               $js = $this->getScriptFunctions() . $this->getToggleScripts();
                $js .= <<<JAVASCRIPT
 ( function( $ ) {
        $.ajaxSetup({ cache: true });
@@ -76,12 +81,22 @@
                                } else {
                                        var geoLocation = Geo.country; // pull 
the geo info
                                }
-                               var bannerList = $.parseJSON( 
wgBannerList[geoLocation] );
-                               $.centralNotice.fn.chooseBanner( bannerList );
+                               var bannerListQuery = $.param( { 'language': 
wgContentLanguage, 'project': wgNoticeProject, 'country': geoLocation } );
+JAVASCRIPT;
+               $js .= "\n\t\t\t\tvar bannerListURL = wgScript + '?title=' + 
encodeURIComponent('" . 
+                       $wgContLang->specialPage( 'BannerListLoader' ) .
+                       "') + '&cache=/cn.js&' + bannerListQuery;\n";
+               $js .= <<<JAVASCRIPT
+                               var request = $.ajax( {
+                                       url: bannerListURL,
+                                       dataType: 'json',
+                                       success: $.centralNotice.fn.chooseBanner
+                               } );
                        },
                        'chooseBanner': function( bannerList ) {
                                // 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;
                                
@@ -122,29 +137,17 @@
                        }
                }
        }
-       // Initialize the query string vars
-       $.centralNotice.fn.getQueryStringVariables();
-       if ( Geo.country ) {
-               // We know the user's country so go ahead and load everything
+       $( document ).ready( function () {
+               // Initialize the query string vars
+               $.centralNotice.fn.getQueryStringVariables();
                if( $.centralNotice.data.getVars['banner'] ) {
-                       // We're forcing one banner
+                       // if we're forcing one banner
                        $.centralNotice.fn.loadBanner( 
$.centralNotice.data.getVars['banner'] );
                } else {
                        // Look for banners ready to go NOW
                        $.centralNotice.fn.loadBannerList( 
$.centralNotice.data.getVars['country'] );
                }
-       } else {
-               // We don't know the user's country yet, so we have to wait for 
the GeoIP lookup
-               $( document ).ready( function () {
-                       if( $.centralNotice.data.getVars['banner'] ) {
-                               // We're forcing one banner
-                               $.centralNotice.fn.loadBanner( 
$.centralNotice.data.getVars['banner'] );
-                       } else {
-                               // Look for banners ready to go NOW
-                               $.centralNotice.fn.loadBannerList( 
$.centralNotice.data.getVars['country'] );
-                       }
-               } ); //document ready
-       }
+       } ); //document ready
 } )( jQuery );
 JAVASCRIPT;
                return $js;
@@ -215,41 +218,5 @@
 JAVASCRIPT;
                return $script;
        }
-       
-       /**
-        * Generate all the banner lists for all the countries
-        */
-       function getAllBannerLists() {
-               $script = "var wgBannerList = new Array();\r\n";
-               $countriesList = CentralNoticeDB::getCountriesList();
-               foreach ( $countriesList as $countryCode => $countryName ) {
-                       $script .= "wgBannerList['$countryCode'] = 
'".$this->getBannerList( $countryCode )."';\r\n";
-               }
-               return $script;
-       }
-       
-       /**
-        * Generate JSON banner list for a given country
-        */
-       function getBannerList( $country ) {
-               global $wgNoticeProject, $wgNoticeLang;
-               $banners = array();
-               
-               // See if we have any preferred campaigns for this language and 
project
-               $campaigns = CentralNoticeDB::getCampaigns( $wgNoticeProject, 
$wgNoticeLang, null, 1, 1, $country );
-               
-               // Quick short circuit to show preferred campaigns
-               if ( $campaigns ) {
-                       // Pull banners
-                       $banners = CentralNoticeDB::getCampaignBanners( 
$campaigns );
-               }
 
-               // Didn't find any preferred banners so do an old style lookup
-               if ( !$banners )  {
-                       $banners = CentralNoticeDB::getBannersByTarget( 
$wgNoticeProject, $wgNoticeLang, $country );
-               }
-               
-               return FormatJson::encode( $banners );
-       }
-
 }

Modified: trunk/extensions/CentralNotice/special/SpecialBannerListLoader.php
===================================================================
--- trunk/extensions/CentralNotice/special/SpecialBannerListLoader.php  
2011-10-11 16:55:07 UTC (rev 99495)
+++ trunk/extensions/CentralNotice/special/SpecialBannerListLoader.php  
2011-10-11 17:02:33 UTC (rev 99496)
@@ -1,7 +1,6 @@
 <?php
 
 /**
- * Note: This file is deprecated and should be deleted if the new banner 
loading system works better.
  * Generates JSON files listing all the banners for a particular site
  */
 class SpecialBannerListLoader extends UnlistedSpecialPage {


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

Reply via email to