Index: trunk/extensions/CentralNotice/CentralNotice.php |
— | — | @@ -30,6 +30,9 @@ |
31 | 31 | // The name of the database which hosts the centralized campaign data |
32 | 32 | $wgCentralDBname = 'metawiki'; |
33 | 33 | |
| 34 | +// The path to Special Pages on the wiki that hosts the CentralNotice infrastructure |
| 35 | +$wgCentralPagePath = 'http://meta.wikimedia.org/wiki/'; |
| 36 | + |
34 | 37 | // Enable the loader itself |
35 | 38 | // Allows to control the loader visibility, without destroying infrastructure |
36 | 39 | // for cached content |
Index: trunk/extensions/CentralNotice/SpecialBannerLoader.php |
— | — | @@ -8,7 +8,7 @@ |
9 | 9 | public $language = 'en'; // User language |
10 | 10 | protected $sharedMaxAge = 900; // Cache for 15 minutes on the server side |
11 | 11 | protected $maxAge = 0; // No client-side banner caching so we get all impressions |
12 | | - protected $contentType = 'text/html'; |
| 12 | + protected $contentType = 'text/js'; |
13 | 13 | |
14 | 14 | function __construct() { |
15 | 15 | // Register special page |
— | — | @@ -27,22 +27,19 @@ |
28 | 28 | // Get site name from the query string |
29 | 29 | $this->siteName = $wgRequest->getText( 'sitename', 'Wikipedia' ); |
30 | 30 | |
31 | | - // If we're not pulling the banner into another page, we'll need to add some extra HTML |
32 | | - $standAlone = $wgRequest->getBool( 'standalone' ); |
33 | | - |
34 | 31 | if ( $wgRequest->getText( 'banner' ) ) { |
35 | 32 | $bannerName = $wgRequest->getText( 'banner' ); |
36 | | - $content = $this->getHtmlNotice( $bannerName, $standAlone ); |
| 33 | + $content = $this->getJsNotice( $bannerName ); |
37 | 34 | if ( preg_match( "/<centralnotice-template-\w{1,}>\z/", $content ) ) { |
38 | | - echo "<!-- Failed cache lookup -->"; |
| 35 | + echo "/* Failed cache lookup */"; |
39 | 36 | } elseif ( strlen( $content ) == 0 ) { |
40 | 37 | // Hack for IE/Mac 0-length keepalive problem, see RawPage.php |
41 | | - echo "<!-- Empty -->"; |
| 38 | + echo "/* Empty */"; |
42 | 39 | } else { |
43 | 40 | echo $content; |
44 | 41 | } |
45 | 42 | } else { |
46 | | - echo "<!-- No banner specified -->"; |
| 43 | + echo "/* No banner specified */"; |
47 | 44 | } |
48 | 45 | } |
49 | 46 | |
— | — | @@ -55,35 +52,38 @@ |
56 | 53 | } |
57 | 54 | |
58 | 55 | /** |
| 56 | + * Generate the JS for the requested banner |
| 57 | + * @return a string of Javascript containing a call to insertBanner() with JSON containing the banner content as the parameter |
| 58 | + */ |
| 59 | + function getJsNotice( $bannerName ) { |
| 60 | + // Make sure the banner exists |
| 61 | + if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) { |
| 62 | + $this->bannerName = $bannerName; |
| 63 | + $bannerHtml = ''; |
| 64 | + $bannerHtml .= preg_replace_callback( |
| 65 | + '/{{{(.*?)}}}/', |
| 66 | + array( $this, 'getNoticeField' ), |
| 67 | + $this->getNoticeTemplate() |
| 68 | + ); |
| 69 | + $bannerArray = array( 'banner' => $bannerHtml ); |
| 70 | + $bannerJs = 'insertBanner('.FormatJson::encode( $bannerArray ).');'; |
| 71 | + return $bannerJs; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
59 | 76 | * Generate the HTML for the requested banner |
60 | 77 | */ |
61 | | - function getHtmlNotice( $bannerName, $standAlone = false ) { |
62 | | - global $wgStylePath; |
63 | | - |
| 78 | + function getHtmlNotice( $bannerName ) { |
64 | 79 | // Make sure the banner exists |
65 | 80 | if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) { |
66 | 81 | $this->bannerName = $bannerName; |
67 | 82 | $bannerHtml = ''; |
68 | | - if ( $standAlone ) { |
69 | | - $bannerHtml .= <<<EOT |
70 | | -<html> |
71 | | -<head> |
72 | | - <script type="text/javascript" src="$wgStylePath/common/jquery.min.js"></script> |
73 | | -</head> |
74 | | -<body> |
75 | | -EOT; |
76 | | - } |
77 | 83 | $bannerHtml .= preg_replace_callback( |
78 | 84 | '/{{{(.*?)}}}/', |
79 | 85 | array( $this, 'getNoticeField' ), |
80 | 86 | $this->getNoticeTemplate() |
81 | 87 | ); |
82 | | - if ( $standAlone ) { |
83 | | - $bannerHtml .= <<<EOT |
84 | | -</body> |
85 | | -</html> |
86 | | -EOT; |
87 | | - } |
88 | 88 | return $bannerHtml; |
89 | 89 | } |
90 | 90 | } |
Index: trunk/extensions/CentralNotice/SpecialBannerController.php |
— | — | @@ -42,6 +42,8 @@ |
43 | 43 | * Generate the body for a static Javascript file |
44 | 44 | */ |
45 | 45 | function getOutput() { |
| 46 | + global $wgCentralPagePath; |
| 47 | + |
46 | 48 | $js = $this->getScriptFunctions() . $this->getToggleScripts(); |
47 | 49 | $js .= <<<EOT |
48 | 50 | ( function( $ ) { |
— | — | @@ -53,14 +55,10 @@ |
54 | 56 | 'loadBanner': function( bannerName ) { |
55 | 57 | // Get the requested banner |
56 | 58 | var bannerPage = 'Special:BannerLoader?banner='+bannerName+'&userlang='+wgContentLanguage+'&sitename='+wgNoticeProject; |
57 | | - var bannerURL = wgArticlePath.replace( '$1', bannerPage ); |
58 | | - var request = $.ajax( { |
59 | | - url: bannerURL, |
60 | | - dataType: 'html', |
61 | | - success: function( data ) { |
62 | | - $.centralNotice.fn.displayBanner( data ); |
63 | | - } |
64 | | - }); |
| 59 | +EOT; |
| 60 | + $js .= "\n\t\t\t\tvar bannerScript = '<script type=\"text/javascript\" src=\"$wgCentralPagePath' + bannerPage + '\"></script>';\n"; |
| 61 | + $js .= <<<EOT |
| 62 | + $( '#siteNotice' ).prepend( '<div id="centralNotice" class="' + ( wgNoticeToggleState ? 'expanded' : 'collapsed' ) + '">'+bannerScript+'</div>' ); |
65 | 63 | }, |
66 | 64 | 'loadBannerList': function( geoOverride ) { |
67 | 65 | var bannerListURL; |
— | — | @@ -108,11 +106,6 @@ |
109 | 107 | selectedBanner.name |
110 | 108 | ); |
111 | 109 | }, |
112 | | - 'displayBanner': function( bannerHTML ) { |
113 | | - // Inject the banner html into the page |
114 | | - $( '#siteNotice' ) |
115 | | - .prepend( '<div id="centralNotice" class="' + ( wgNoticeToggleState ? 'expanded' : 'collapsed' ) + '">' + bannerHTML + '</div>' ); |
116 | | - }, |
117 | 110 | 'getQueryStringVariables': function() { |
118 | 111 | document.location.search.replace( /\??(?:([^=]+)=([^&]*)&?)/g, function () { |
119 | 112 | function decode( s ) { |
— | — | @@ -158,6 +151,9 @@ |
159 | 152 | |
160 | 153 | function getScriptFunctions() { |
161 | 154 | $script = " |
| 155 | +function insertBanner(bannerJson) { |
| 156 | + jQuery('div#centralNotice').prepend( bannerJson.banner ); |
| 157 | +} |
162 | 158 | function toggleNotice() { |
163 | 159 | var notice = document.getElementById('centralNotice'); |
164 | 160 | if (!wgNoticeToggleState) { |