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

Revision: 73554
Author:   kaldari
Date:     2010-09-22 18:09:47 +0000 (Wed, 22 Sep 2010)

Log Message:
-----------
switching to JSONP for retrieving banners, getting rid of stand-alone param for 
banners (was only needed for testing)

Modified Paths:
--------------
    trunk/extensions/CentralNotice/CentralNotice.php
    trunk/extensions/CentralNotice/SpecialBannerController.php
    trunk/extensions/CentralNotice/SpecialBannerLoader.php

Modified: trunk/extensions/CentralNotice/CentralNotice.php
===================================================================
--- trunk/extensions/CentralNotice/CentralNotice.php    2010-09-22 18:09:36 UTC 
(rev 73553)
+++ trunk/extensions/CentralNotice/CentralNotice.php    2010-09-22 18:09:47 UTC 
(rev 73554)
@@ -30,6 +30,9 @@
 // The name of the database which hosts the centralized campaign data
 $wgCentralDBname = 'metawiki';
 
+// The path to Special Pages on the wiki that hosts the CentralNotice 
infrastructure
+$wgCentralPagePath = 'http://meta.wikimedia.org/wiki/';
+
 // Enable the loader itself
 // Allows to control the loader visibility, without destroying infrastructure
 // for cached content

Modified: trunk/extensions/CentralNotice/SpecialBannerController.php
===================================================================
--- trunk/extensions/CentralNotice/SpecialBannerController.php  2010-09-22 
18:09:36 UTC (rev 73553)
+++ trunk/extensions/CentralNotice/SpecialBannerController.php  2010-09-22 
18:09:47 UTC (rev 73554)
@@ -42,6 +42,8 @@
         * Generate the body for a static Javascript file
         */
        function getOutput() {
+               global $wgCentralPagePath;
+               
                $js = $this->getScriptFunctions() . $this->getToggleScripts();
                $js .= <<<EOT
 ( function( $ ) {
@@ -53,14 +55,10 @@
                        'loadBanner': function( bannerName ) {
                                // Get the requested banner
                                var bannerPage = 
'Special:BannerLoader?banner='+bannerName+'&userlang='+wgContentLanguage+'&sitename='+wgNoticeProject;
-                               var bannerURL = wgArticlePath.replace( '$1', 
bannerPage );
-                               var request = $.ajax( {
-                                       url: bannerURL,
-                                       dataType: 'html',
-                                       success: function( data ) {
-                                               
$.centralNotice.fn.displayBanner( data );
-                                       }
-                               });
+EOT;
+               $js .= "\n\t\t\t\tvar bannerScript = '<script 
type=\"text/javascript\" src=\"$wgCentralPagePath' + bannerPage + 
'\"></script>';\n";
+               $js .= <<<EOT
+                               $( '#siteNotice' ).prepend( '<div 
id="centralNotice" class="' + ( wgNoticeToggleState ? 'expanded' : 'collapsed' 
) + '">'+bannerScript+'</div>' );
                        },
                        'loadBannerList': function( geoOverride ) {
                                var bannerListURL;
@@ -108,11 +106,6 @@
                                        selectedBanner.name
                                );
                        },
-                       'displayBanner': function( bannerHTML ) {
-                               // Inject the banner html into the page
-                               $( '#siteNotice' )
-                                       .prepend( '<div id="centralNotice" 
class="' + ( wgNoticeToggleState ? 'expanded' : 'collapsed' ) + '">' + 
bannerHTML + '</div>' );
-                       },
                        'getQueryStringVariables': function() {
                                document.location.search.replace( 
/\??(?:([^=]+)=([^&]*)&?)/g, function () {
                                        function decode( s ) {
@@ -158,6 +151,9 @@
 
        function getScriptFunctions() {
                $script = "
+function insertBanner(bannerJson) {
+       jQuery('div#centralNotice').prepend( bannerJson.banner );
+}
 function toggleNotice() {
        var notice = document.getElementById('centralNotice');
        if (!wgNoticeToggleState) {

Modified: trunk/extensions/CentralNotice/SpecialBannerLoader.php
===================================================================
--- trunk/extensions/CentralNotice/SpecialBannerLoader.php      2010-09-22 
18:09:36 UTC (rev 73553)
+++ trunk/extensions/CentralNotice/SpecialBannerLoader.php      2010-09-22 
18:09:47 UTC (rev 73554)
@@ -8,7 +8,7 @@
        public $language = 'en'; // User language
        protected $sharedMaxAge = 900; // Cache for 15 minutes on the server 
side
        protected $maxAge = 0; // No client-side banner caching so we get all 
impressions
-       protected $contentType = 'text/html';
+       protected $contentType = 'text/js';
        
        function __construct() {
                // Register special page
@@ -27,22 +27,19 @@
                // Get site name from the query string
                $this->siteName = $wgRequest->getText( 'sitename', 'Wikipedia' 
);
                
-               // If we're not pulling the banner into another page, we'll 
need to add some extra HTML
-               $standAlone = $wgRequest->getBool( 'standalone' );
-               
                if ( $wgRequest->getText( 'banner' ) ) {
                        $bannerName = $wgRequest->getText( 'banner' );
-                       $content = $this->getHtmlNotice( $bannerName, 
$standAlone );
+                       $content = $this->getJsNotice( $bannerName );
                        if ( preg_match( 
"/&lt;centralnotice-template-\w{1,}&gt;\z/", $content ) ) {
-                               echo "<!-- Failed cache lookup -->";
+                               echo "/* Failed cache lookup */";
                        } elseif ( strlen( $content ) == 0 ) {
                                // Hack for IE/Mac 0-length keepalive problem, 
see RawPage.php
-                               echo "<!-- Empty -->";
+                               echo "/* Empty */";
                        } else {
                                echo $content;
                        }
                } else {
-                       echo "<!-- No banner specified -->";
+                       echo "/* No banner specified */";
                }
        }
        
@@ -55,35 +52,38 @@
        }
        
        /**
+        * Generate the JS for the requested banner
+        * @return a string of Javascript containing a call to insertBanner() 
with JSON containing the banner content as the parameter
+        */
+       function getJsNotice( $bannerName ) {
+               // Make sure the banner exists
+               if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) {
+                       $this->bannerName = $bannerName;
+                       $bannerHtml = '';
+                       $bannerHtml .= preg_replace_callback(
+                               '/{{{(.*?)}}}/',
+                               array( $this, 'getNoticeField' ),
+                               $this->getNoticeTemplate()
+                       );
+                       $bannerArray = array( 'banner' => $bannerHtml );
+                       $bannerJs = 'insertBanner('.FormatJson::encode( 
$bannerArray ).');';
+                       return $bannerJs;
+               }
+       }
+       
+       /**
         * Generate the HTML for the requested banner
         */
-       function getHtmlNotice( $bannerName, $standAlone = false ) {
-               global $wgStylePath;
-               
+       function getHtmlNotice( $bannerName ) {
                // Make sure the banner exists
                if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) {
                        $this->bannerName = $bannerName;
                        $bannerHtml = '';
-                       if ( $standAlone ) {
-                               $bannerHtml .= <<<EOT
-<html>
-<head>
-       <script type="text/javascript" 
src="$wgStylePath/common/jquery.min.js"></script>
-</head>
-<body>
-EOT;
-                       }
                        $bannerHtml .= preg_replace_callback(
                                '/{{{(.*?)}}}/',
                                array( $this, 'getNoticeField' ),
                                $this->getNoticeTemplate()
                        );
-                       if ( $standAlone ) {
-                               $bannerHtml .= <<<EOT
-</body>
-</html>
-EOT;
-                       }
                        return $bannerHtml;
                }
        }



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

Reply via email to