Ori.livneh has uploaded a new change for review.

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

Change subject: Set the GeoIP cookie client-side even if request failed
......................................................................

Set the GeoIP cookie client-side even if request failed

To prevent honest-to-goodness IPv6 users from having to try and fail to resolve
the adress of our IPv4-only honey trap on every single request, cache the
result is a cookie.

Change-Id: I1295052b5e409274524f1b78497e09a8233e2c12
---
M modules/ext.centralNotice.bannerController/bannerController.js
1 file changed, 38 insertions(+), 15 deletions(-)


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

diff --git a/modules/ext.centralNotice.bannerController/bannerController.js 
b/modules/ext.centralNotice.bannerController/bannerController.js
index 02d8d36..7f6d7b4 100644
--- a/modules/ext.centralNotice.bannerController/bannerController.js
+++ b/modules/ext.centralNotice.bannerController/bannerController.js
@@ -36,11 +36,32 @@
                }
        }
 
-       if ( typeof window.Geo !== 'object' ) {
-               window.Geo = ( function ( match, country, city, lat, lon, af ) {
-                       return { country: country, city: city, lat: lat, lon: 
lon, af: af };
-               } ).apply( null, document.cookie.match( 
/(?:\bGeoIP=)([^:]*):([^:]*):([^:]*):([^:]*):([^;]*)/ ) || [] );
+       function synthesizeGeoCookie() {
+               if ( !window.Geo || !window.Geo.country ) {
+                       $.cookie( 'GeoIP', 'GeoIP=::::vx', { path: '/' } );
+                       return;
+               }
+
+               var parts = [
+                       window.Geo.country,
+                       window.Geo.city.replace( /[^a-z]/i, '_' ),
+                       window.Geo.lat,
+                       window.Geo.lon,
+                       ( window.Geo.IP && window.Geo.IP.match(':') ) ? 'v6' : 
'v4'
+               ];
+
+               $.cookie( 'GeoIP', parts.join( ':' ), { path: '/' } );
        }
+
+       window.Geo = ( function ( match, country, city, lat, lon, af ) {
+               return {
+                       country: country,
+                       city: city,
+                       lat: lat && parseFloat( lat ),
+                       lon: lon && parseFloat( lon ),
+                       af: af
+               };
+       } ).apply( null, document.cookie.match( 
/(?:\bGeoIP=)([^:]*):([^:]*):([^:]*):([^:]*):([^;]*)/ || [] ) );
 
        mw.centralNotice = {
                /**
@@ -201,20 +222,22 @@
                        // If the user has no country assigned, we try a new 
lookup via
                        // geoiplookup.wikimedia.org. This hostname has no IPv6 
address,
                        // so will force dual-stack users to fall back to IPv4.
-                       if ( mw.centralNotice.data.country === 'XX' && 
mw.centralNotice.data.addressFamily === 'IPv6' ) {
-                               $.ajax({
+                       if ( mw.centralNotice.data.country === 'XX' ) {// && 
mw.centralNotice.data.addressFamily === 'IPv6' ) {
+                               $.ajax( {
                                        url: '//geoiplookup.wikimedia.org/',
                                        dataType: 'script',
-                                       cache: true,
-                                       complete: function() {
-                                               if ( window.Geo.country ) {
-                                                       
mw.centralNotice.data.country = window.Geo.country;
-                                               } else {
-                                                       
mw.centralNotice.data.country = 'XX';
-                                               }
-                                               mw.centralNotice.loadBanner();
+                                       cache: true
+                               } ).always( function () {
+                                       if ( window.Geo && window.Geo.country ) 
{
+                                               mw.centralNotice.data.country = 
window.Geo.country;
+                                       } else {
+                                               mw.centralNotice.data.country = 
'XX';
                                        }
-                               });
+                                       // Set a session cookie so that 
subsequent page views neither trigger
+                                       // an IP lookup in Varnish nor an AJAX 
request to geoiplookup.
+                                       synthesizeGeoCookie();
+                                       mw.centralNotice.loadBanner();
+                               } );
                        } else {
                                mw.centralNotice.loadBanner();
                        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1295052b5e409274524f1b78497e09a8233e2c12
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

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

Reply via email to