Awight has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/181244

Change subject: WIP Preload JS comes with no baggage
......................................................................

WIP Preload JS comes with no baggage

It's just a snippet that runs before the banner is inserted.  There is no
mandatory interface such as implementing the alterImpressionData hook or
returning a boolean.

Change-Id: I8ffcbb44ac15cbb91dad6786cc2055f589174d1d
---
M includes/BannerRenderer.php
D mixins/BannerDiet/BannerDiet.js
M mixins/BannerShowHideCountDate/BannerShowHideCountDate.js
M modules/ext.centralNotice.bannerController/bannerController.js
M special/SpecialBannerLoader.php
5 files changed, 29 insertions(+), 44 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralNotice 
refs/changes/44/181244/1

diff --git a/includes/BannerRenderer.php b/includes/BannerRenderer.php
index b42c772..308391d 100644
--- a/includes/BannerRenderer.php
+++ b/includes/BannerRenderer.php
@@ -110,31 +110,25 @@
        }
 
        function getPreloadJs() {
-               return $this->substituteMagicWords( $this->getPreloadJsRaw() );
+               $code = $this->substituteMagicWords( $this->getPreloadJsRaw() );
+
+               // Minify the code, if any.
+               if ( !$this->debug && $code ) {
+                       $code = JavaScriptMinifier::minify( $code );
+               }
+               return $code;
        }
 
        function getPreloadJsRaw() {
                $snippets = $this->mixinController->getPreloadJsSnippets();
-               $bundled = array();
-               $bundled[] = 'var retval = 1;';
 
-               if ( $snippets ) {
-                       foreach ( $snippets as $mixin => $code ) {
-                               if ( !$this->debug ) {
-                                       $code = JavaScriptMinifier::minify( 
$code );
-                               }
-
-                               // Uses bitwise AND to assert all snippets 
returned 1
-                               $bundled[] = "/* {$mixin}: */ retval &= 
{$code}";
-                       }
-               }
-               $bundled[] = 'return retval;';
-               return implode( "\n", $bundled );
+               return implode( "\n\n", $snippets );
        }
 
        function getResourceLoaderHtml() {
                $modules = $this->mixinController->getResourceLoaderModules();
                if ( $modules ) {
+                       // FIXME: Does the RL library already include a helper 
to do this?
                        $html = "<!-- " . implode( ", ", array_keys( $modules ) 
) . " -->";
                        $html .= Html::inlineScript(
                                ResourceLoader::makeLoaderConditionalScript(
diff --git a/mixins/BannerDiet/BannerDiet.js b/mixins/BannerDiet/BannerDiet.js
deleted file mode 100644
index daa716c..0000000
--- a/mixins/BannerDiet/BannerDiet.js
+++ /dev/null
@@ -1,16 +0,0 @@
-( function( $ ) {
-       // Returns into a bit field (See BannerRenderer.php).
-
-       if ( location.search.match( /\breset=1/ ) ) {
-               $.cookie( '{{{hide-cookie-name}}}', 0, { expires: 365, path: 
'/' } );
-               return 1;
-       }
-       var cookieCount = parseInt( $.cookie( '{{{hide-cookie-name}}}' ), 10 ) 
| 0;
-
-       if ( cookieCount < parseInt( '{{{hide-cookie-max-count}}}', 10 ) ) {
-               $.cookie( '{{{hide-cookie-name}}}', cookieCount + 1, { expires: 
365, path: '/' } );
-               return 1;
-       } else {
-               return 0;
-       }
-} )( jQuery );
diff --git a/mixins/BannerShowHideCountDate/BannerShowHideCountDate.js 
b/mixins/BannerShowHideCountDate/BannerShowHideCountDate.js
index 7715a5e..509979c 100644
--- a/mixins/BannerShowHideCountDate/BannerShowHideCountDate.js
+++ b/mixins/BannerShowHideCountDate/BannerShowHideCountDate.js
@@ -79,9 +79,9 @@
   $.cookie('{{{hide-cookie-name}}}', cookieCount, { expires: 365, path: '/' });
   $.cookie('{{{hide-cookie-name}}}-wait', waitData, { expires: 365, path: '/' 
});
 
-  mw.centralNotice.impressionData.result = hideBanner;
-  mw.centralNotice.impressionData.reason = hideReason;
   mw.centralNotice.impressionData.banner_count = cookieCount;
-
-  return !hideBanner;
+  if ( hideBanner ) {
+       mw.centralNotice.impressionData.result = 'hide';
+       mw.centralNotice.impressionData.reason = hideReason;
+  }
 })( mediaWiki, jQuery );
diff --git a/modules/ext.centralNotice.bannerController/bannerController.js 
b/modules/ext.centralNotice.bannerController/bannerController.js
index f142ec2..516f8d5 100644
--- a/modules/ext.centralNotice.bannerController/bannerController.js
+++ b/modules/ext.centralNotice.bannerController/bannerController.js
@@ -406,6 +406,8 @@
                                // along with its start and end dates.
                                // However we won't do this when a banner is 
being forced via
                                // the banner URL param.
+                               // FIXME: This seems sketchy, maybe we should 
just confess that
+                               // buckets were not involved?
                                bucket = 
mw.cnBannerControllerLib.bucketsByCampaign[impressionData.campaign];
                                impressionData.bucket = bucket.val;
                                impressionData.bucketStart = bucket.start;
@@ -415,14 +417,19 @@
                        // Get the banner type for more queryness
                        mw.centralNotice.data.category = encodeURIComponent( 
bannerJson.category );
 
-                       if ( typeof mw.centralNotice.bannerData.preload === 
'function'
-                               && !mw.centralNotice.bannerData.preload()
-                       ) {
+                       // Check whether inline (preloaded) javascript has 
suppressed this
+                       // banner.
+                       if ( impressionData.result === 'hide' ) {
                                hideBanner = true;
+                               // Fill in the reason in case the preload code 
forgot to do so.
                                if ( !impressionData.reason ) {
                                        impressionData.reason = 'preload';
                                }
-                       } else if ( mw.centralNotice.data.testing === false ) { 
/* And we want to see what we're testing! :) */
+                       }
+
+                       // Do some builtin hide cookie things, unless we're 
testing.
+                       // TODO: Push this into a mixin.
+                       if ( !hideBanner && mw.centralNotice.data.testing === 
false ) {
                                cookieName = 'centralnotice_hide_' + 
mw.centralNotice.data.category;
                                cookieVal = $.cookie( cookieName );
                                durations = mw.config.get( 
'wgNoticeCookieDurations' );
@@ -448,6 +455,7 @@
                                        }
                                }
                        }
+
                        if ( !hideBanner ) {
                                // Not hidden yet, inject the banner
                                mw.centralNotice.bannerData.bannerName = 
bannerJson.bannerName;
@@ -498,7 +506,9 @@
                        }
                }
 
-               impressionData.result = hideBanner ? 'hide' : 'show';
+               if ( !impressionData.result ) {
+                       impressionData.result = hideBanner ? 'hide' : 'show';
+               }
 
                if ( !mw.centralNotice.data.testing ) {
                        mw.centralNotice.recordImpression( impressionData );
diff --git a/special/SpecialBannerLoader.php b/special/SpecialBannerLoader.php
index 8ab1229..4fe7a6d 100644
--- a/special/SpecialBannerLoader.php
+++ b/special/SpecialBannerLoader.php
@@ -125,11 +125,8 @@
                $bannerJson = FormatJson::encode( $bannerArray );
 
                $preload = $bannerRenderer->getPreloadJs();
-               if ( $preload ) {
-                       $preload = "mw.centralNotice.bannerData.preload = 
function() { {$preload} };";
-               }
 
-               $bannerJs = $preload . "mw.centralNotice.insertBanner( 
{$bannerJson} );";
+               $bannerJs = "{$preload}\nmw.centralNotice.insertBanner( 
{$bannerJson} );";
 
                return $bannerJs;
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/181244
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8ffcbb44ac15cbb91dad6786cc2055f589174d1d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>

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

Reply via email to