r73554 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73553‎ | r73554 | r73555 >
Date:18:09, 22 September 2010
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialBannerController.php (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialBannerLoader.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/CentralNotice.php
@@ -30,6 +30,9 @@
3131 // The name of the database which hosts the centralized campaign data
3232 $wgCentralDBname = 'metawiki';
3333
 34+// The path to Special Pages on the wiki that hosts the CentralNotice infrastructure
 35+$wgCentralPagePath = 'http://meta.wikimedia.org/wiki/';
 36+
3437 // Enable the loader itself
3538 // Allows to control the loader visibility, without destroying infrastructure
3639 // for cached content
Index: trunk/extensions/CentralNotice/SpecialBannerLoader.php
@@ -8,7 +8,7 @@
99 public $language = 'en'; // User language
1010 protected $sharedMaxAge = 900; // Cache for 15 minutes on the server side
1111 protected $maxAge = 0; // No client-side banner caching so we get all impressions
12 - protected $contentType = 'text/html';
 12+ protected $contentType = 'text/js';
1313
1414 function __construct() {
1515 // Register special page
@@ -27,22 +27,19 @@
2828 // Get site name from the query string
2929 $this->siteName = $wgRequest->getText( 'sitename', 'Wikipedia' );
3030
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 -
3431 if ( $wgRequest->getText( 'banner' ) ) {
3532 $bannerName = $wgRequest->getText( 'banner' );
36 - $content = $this->getHtmlNotice( $bannerName, $standAlone );
 33+ $content = $this->getJsNotice( $bannerName );
3734 if ( preg_match( "/<centralnotice-template-\w{1,}>\z/", $content ) ) {
38 - echo "<!-- Failed cache lookup -->";
 35+ echo "/* Failed cache lookup */";
3936 } elseif ( strlen( $content ) == 0 ) {
4037 // Hack for IE/Mac 0-length keepalive problem, see RawPage.php
41 - echo "<!-- Empty -->";
 38+ echo "/* Empty */";
4239 } else {
4340 echo $content;
4441 }
4542 } else {
46 - echo "<!-- No banner specified -->";
 43+ echo "/* No banner specified */";
4744 }
4845 }
4946
@@ -55,35 +52,38 @@
5653 }
5754
5855 /**
 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+ /**
5976 * Generate the HTML for the requested banner
6077 */
61 - function getHtmlNotice( $bannerName, $standAlone = false ) {
62 - global $wgStylePath;
63 -
 78+ function getHtmlNotice( $bannerName ) {
6479 // Make sure the banner exists
6580 if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) {
6681 $this->bannerName = $bannerName;
6782 $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 - }
7783 $bannerHtml .= preg_replace_callback(
7884 '/{{{(.*?)}}}/',
7985 array( $this, 'getNoticeField' ),
8086 $this->getNoticeTemplate()
8187 );
82 - if ( $standAlone ) {
83 - $bannerHtml .= <<<EOT
84 -</body>
85 -</html>
86 -EOT;
87 - }
8888 return $bannerHtml;
8989 }
9090 }
Index: trunk/extensions/CentralNotice/SpecialBannerController.php
@@ -42,6 +42,8 @@
4343 * Generate the body for a static Javascript file
4444 */
4545 function getOutput() {
 46+ global $wgCentralPagePath;
 47+
4648 $js = $this->getScriptFunctions() . $this->getToggleScripts();
4749 $js .= <<<EOT
4850 ( function( $ ) {
@@ -53,14 +55,10 @@
5456 'loadBanner': function( bannerName ) {
5557 // Get the requested banner
5658 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>' );
6563 },
6664 'loadBannerList': function( geoOverride ) {
6765 var bannerListURL;
@@ -108,11 +106,6 @@
109107 selectedBanner.name
110108 );
111109 },
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 - },
117110 'getQueryStringVariables': function() {
118111 document.location.search.replace( /\??(?:([^=]+)=([^&]*)&?)/g, function () {
119112 function decode( s ) {
@@ -158,6 +151,9 @@
159152
160153 function getScriptFunctions() {
161154 $script = "
 155+function insertBanner(bannerJson) {
 156+ jQuery('div#centralNotice').prepend( bannerJson.banner );
 157+}
162158 function toggleNotice() {
163159 var notice = document.getElementById('centralNotice');
164160 if (!wgNoticeToggleState) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r73571use $wgJsMimeType, ensure cachability of JSONP requests, escape $wgCentralPag...kaldari21:03, 22 September 2010

Comments

#Comment by Nikerabbit (talk | contribs)   19:28, 22 September 2010

Shouldn't $wgCentralPagePath be escaped before outputted as JavaScript?

+	protected $contentType = 'text/js';

Is there such mime type? Can $wgJsMimeType be used instead?

#Comment by Kaldari (talk | contribs)   21:03, 22 September 2010

fixed in r73571. Thanks for the feedback!

#Comment by Kaldari (talk | contribs)   21:11, 22 September 2010

Once we're using jQuery 1.4 we'll be able to do the jsonp request in a much more elegant fashion.

var request = $.ajax( {
	url: bannerURL,
	dataType: 'jsonp',
	jsonpCallback: 'parseBanner',
	cache: true,
	success: $.centralNotice.fn.displayBanner
});

Apparently, in jQuery 1.3 you can't override the jsonp callback function name, and instead it is randomly generated, which breaks caching.

#Comment by MZMcBride (talk | contribs)   01:36, 7 October 2010

jQuery 1.4 should be along next week.

#Comment by Catrope (talk | contribs)   20:05, 27 September 2010
+		$js .= "\n\t\t\t\tvar bannerScript = '<script type=\"text/javascript\" src=\"$wgCentralPagePath' + bannerPage + '\"></script>';\n";

If you export this var using the MakeGlobalVariables hook, you can put this JS in a static file.

#Comment by Kaldari (talk | contribs)   23:38, 5 January 2011

Personally, I'd rather leave it in the PHP than add another global JS var. I know it's not pretty, but it works and isn't totally unreadable :)

#Comment by Krinkle (talk | contribs)   23:50, 5 January 2011
+// The path to Special Pages on the wiki that hosts the CentralNotice infrastructure
+$wgCentralPagePath = '[http://meta.wikimedia.org/wiki/'; http://meta.wikimedia.org/wiki/';]
+
(..)
 			var bannerPage = 'Special:BannerLoader?banner='+bannerName+'&userlang='+wgContentLanguage+'&sitename='+wgNoticeProject;
(..)
+	$js .= "\n\t\t\t\tvar bannerScript = '<script type=\"text/javascript\" src=\"$wgCentralPagePath' + bannerPage + '\"></script>';\n";

From the name I would think $wgCentralPagePath should include 'Special:BannerLoader' as well. Or is it used in other places as well ?

#Comment by Kaldari (talk | contribs)   00:46, 6 January 2011

$wgCentralPagePath is currently only used for generating the BannerLoader URL, but it could be useful for other things in the future. BTW, the value for $wgCentralPagePath was replaced with a null string in r73963 since it makes more sense to set this in the local config rather than within CentralNotice.

Status & tagging log