diff --git a/UniversalLanguageSelector.hooks.php b/UniversalLanguageSelector.hooks.php index 2affdf46..53dfe65b 100644 --- a/UniversalLanguageSelector.hooks.php +++ b/UniversalLanguageSelector.hooks.php @@ -121,10 +121,8 @@ class UniversalLanguageSelectorHooks { $out->addModules( 'ext.uls.compactlinks' ); } - if ( is_string( $wgULSGeoService ) ) { + if ( $wgULSGeoService ) { $out->addModules( 'ext.uls.geoclient' ); - } elseif ( $wgULSGeoService === true ) { - $out->addScript( '' ); } if ( self::isToolbarEnabled( $out->getUser() ) ) { @@ -312,7 +310,11 @@ class UniversalLanguageSelectorHooks { $wgWBClientSettings; // Place constant stuff here (not depending on request context) - if ( is_string( $wgULSGeoService ) ) { + + if ( $wgULSGeoService === true ) { + $wgULSGeoService = 'https://freegeoip.net/json/?callback=?'; + } + if ( $wgULSGeoService ) { $vars['wgULSGeoService'] = $wgULSGeoService; } diff --git a/extension.json b/extension.json index c0479d0e..64c993a8 100644 --- a/extension.json +++ b/extension.json @@ -55,7 +55,7 @@ "compact-language-links": true }, "config": { - "@ULSGeoService": "ULS can use geolocation services to suggest languages based on the country the user is vising from. Setting this to false will prevent builtin geolocation from being used. You can provide your own geolocation by setting window.Geo to object which has key \"country_code\" or \"country\". If set to true, it will query Wikimedia's geoip service. The service should return jsonp that uses the supplied callback parameter.", + "@ULSGeoService": "ULS can use a geolocation service to suggest languages based on the country the user is vising from. If set to true, it will query a free geoip service. Setting this to false will prevent built-in geolocation from being used. You can provide your own geolocation by setting window.Geo to an object which has key \"country_code\" or \"country\".", "ULSGeoService": true, "@ULSEnable": "Enable language selection, compact language links, input methods and webfonts for everyone, unless the behavior is overridden by the configuration variables below. Even if false the classes and resource loader modules are registered for the use of other extensions. Language changing via cookie or setlang query parameter is not possible.", "ULSEnable": true, @@ -129,6 +129,10 @@ }, "ext.uls.geoclient": { "scripts": "js/ext.uls.geoclient.js", + "dependencies": [ + "json", + "mediawiki.cookie" + ], "localBasePath": "resources", "remoteExtPath": "UniversalLanguageSelector/resources" }, diff --git a/resources/js/ext.uls.geoclient.js b/resources/js/ext.uls.geoclient.js index 71d2de61..52937dee 100644 --- a/resources/js/ext.uls.geoclient.js +++ b/resources/js/ext.uls.geoclient.js @@ -20,29 +20,29 @@ ( function ( $, mw ) { 'use strict'; - var currentProto, httpOnly, settings, + var geo, + cacheAge = 60 * 60 * 8, // 8 hours service = mw.config.get( 'wgULSGeoService' ); - mw.uls = mw.uls || {}; - mw.uls.setGeo = function ( data ) { - window.Geo = data; - }; - - // Call the service only if defined, and if the current - // protocol is https, only if the service is not configured - // with http:// as the protocol - if ( service ) { - httpOnly = service.substring( 0, 7 ) === 'http://'; - currentProto = document.location.protocol; - if ( !httpOnly || currentProto === 'http:' ) { - settings = { - cache: true, - dataType: 'jsonp', - jsonpCallback: 'mw.uls.setGeo' - }; - - $.ajax( service, settings ); - } + // This is not supposed to happen. For sanity prefer existing value. + if ( window.Geo ) { + return; } + // Using cache for speed and to be nice for the service. Using cookies over + // localstorage because cookies support automatic expiring and are supported + // everywhere where as localstorage might not. This cookie is not currently + // read server side. + geo = mw.cookie.get( 'ULSGeo' ); + if ( geo ) { + try { + window.Geo = JSON.parse( geo ); + return; + } catch ( e ) {} + } + + $.getJSON( service ).done( function ( data ) { + window.Geo = data; + mw.cookie.set( 'ULSGeo', JSON.stringify( data ), cacheAge ); + } ); }( jQuery, mediaWiki ) );