Files
mediawiki-extensions-Univer…/resources/js/ext.uls.init.js
Ori Livneh 1603fd97a8 Use $.Callbacks to queue events until EL has loaded
This patch provides a workaround for bug 50746. It modifies mw.uls.eventLog so
that it does not directly attempt to log events but instead adds them to a
$.Callbacks('memory once') queue. If EventLogging is not enabled, the Callbacks
object never fires. If EventLogging is enabled, it is requested via
mw.loader.using, but only after $(document).ready. This prevents ResourceLoader
from attempting to inject it via document.write. The mw.loader.using callback
sets the schema defaults and calls .fire on the Callbacks object, causing the
EventLogging event queue to be processed. Tested on Opera 12 and Chrome 30 on
OS X.

Bug: 50746
Change-Id: I89cf06990ee85e70698149b4812d7900218e4fb4
2013-07-09 01:48:06 +00:00

274 lines
7.5 KiB
JavaScript

/**
* ULS startup script - MediaWiki specific customization for jquery.uls
*
* Copyright (C) 2012-2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
* Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other
* contributors. See CREDITS for a list.
*
* UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't
* have to do anything special to choose one license or the other and you don't
* have to notify anyone which license you are using. You are free to use
* UniversalLanguageSelector in commercial projects as long as the copyright
* header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
*
* @file
* @ingroup Extensions
* @licence GNU General Public Licence 2.0 or later
* @licence MIT License
*/
( function ( $, mw, undefined ) {
'use strict';
// MediaWiki override for ULS defaults.
$.fn.uls.defaults = $.extend( $.fn.uls.defaults, {
languages: mw.config.get( 'wgULSLanguages' ),
searchAPI: mw.util.wikiScript( 'api' ) + '?action=languagesearch'
} );
// No need of IME in language search bar of ULS
$.fn.uls.Constructor.prototype.render = function () {
this.$languageFilter.addClass( 'noime' );
};
var MWMessageStore,
jsonLoader,
initialized = false,
currentLang = mw.config.get( 'wgUserLanguage' ),
logEventQueue = $.Callbacks( 'memory once' );
mw.uls = mw.uls || {};
mw.uls.previousLanguagesCookie = 'uls-previous-languages';
/**
* Change the language of wiki using setlang URL parameter
* @param {String} language
*/
mw.uls.changeLanguage = function ( language ) {
var uri = new mw.Uri( window.location.href );
uri.extend( {
setlang: language
} );
window.location.href = uri.toString();
};
mw.uls.setPreviousLanguages = function ( previousLanguages ) {
$.cookie( mw.uls.previousLanguagesCookie, $.toJSON( previousLanguages ) );
};
mw.uls.getPreviousLanguages = function () {
var previousLanguages = $.cookie( mw.uls.previousLanguagesCookie );
if ( !previousLanguages ) {
return [];
}
// return last 5 language changes
return $.parseJSON( previousLanguages ).slice( -5 );
};
mw.uls.getBrowserLanguage = function () {
return ( window.navigator.language || window.navigator.userLanguage ).split( '-' )[0];
};
/*jshint camelcase:false*/
mw.uls.getCountryCode = function () {
return window.Geo && ( window.Geo.country || window.Geo.country_code );
};
mw.uls.getAcceptLanguageList = function () {
return mw.config.get( 'wgULSAcceptLanguageList' );
};
/**
* Get a list of codes for languages to show in
* the "Common languages" section of the ULS.
* The list consists of the user's current selected language,
* the wiki's content language, the browser' UI language
* and Accept-Language, user's previous selected languages
* and finally, the languages of countryCode taken from the CLDR,
* taken by default from the user's geolocation.
*
* @param {String} [countryCode] Uppercase country code.
* @return {Array} List of language codes without duplicates.
*/
mw.uls.getFrequentLanguageList = function ( countryCode ) {
var unique = [],
list = [
mw.config.get( 'wgUserLanguage' ),
mw.config.get( 'wgContentLanguage' ),
mw.uls.getBrowserLanguage()
]
.concat( mw.uls.getPreviousLanguages() )
.concat( mw.uls.getAcceptLanguageList() );
countryCode = countryCode || mw.uls.getCountryCode();
if ( countryCode ) {
list = list.concat( $.uls.data.getLanguagesInTerritory( countryCode ) );
}
$.each( list, function ( i, v ) {
if ( $.inArray( v, unique ) === -1 ) {
unique.push( v );
}
} );
// Filter out unknown and unsupported languages
unique = $.grep( unique, function ( langCode ) {
var target;
// If the language is already known and defined, just use it
if ( $.fn.uls.defaults.languages[langCode] !== undefined ) {
return true;
}
// If the language is not immediately known,
// try to check is as a redirect
target = $.uls.data.isRedirect( langCode );
if ( target ) {
// Check that the redirect's target is known
// to this instance of ULS
return $.fn.uls.defaults.languages[target] !== undefined;
}
return false;
} );
return unique;
};
/**
* Checks whether the browser is supported.
* Browse support policy: http://www.mediawiki.org/wiki/Browser_support#Grade_A
* @return boolean
*/
function isBrowserSupported() {
// Blacklist Grade B browsers IE 6, 7 and IE60-IE79
return !/MSIE [67]/i.test( navigator.userAgent );
}
/**
* Local wrapper for 'mw.eventLog.logEvent' which handles default params
* and ensures the correct schema is loaded.
*
* @param {Object} data Event action and optional fields
* @since 2013.07
* @see https://meta.wikimedia.org/wiki/Schema:UniversalLanguageSelector
*/
mw.uls.logEvent = function ( event ) {
logEventQueue.add( function () {
mw.eventLog.logEvent( 'UniversalLanguageSelector', event );
} );
};
/**
* jquery.i18n message store for MediaWiki
*
*/
MWMessageStore = function () {
this.messages = {};
};
MWMessageStore.prototype = {
init: function () {},
get: function ( locale, messageKey ) {
return ( this.isLoaded( locale ) && this.messages[locale][messageKey] ) ||
'<' + messageKey + '>';
},
set: function( locale, messages ) {
this.messages[locale] = messages;
},
isLoaded: function ( locale ) {
return this.messages[locale];
},
load: function ( locale ) {
var store = this,
deferred = $.Deferred(),
url = mw.util.wikiScript( 'api' ) + '?action=ulslocalization&language=';
if ( store.isLoaded( locale ) ) {
return deferred.resolve();
}
deferred = $.getJSON( url + locale ).done( function ( data ) {
store.set( locale, data );
} ).fail( function ( jqxhr, settings, exception ) {
mw.log( 'Error in loading messages from ' + url + ' Exception: ' + exception );
} );
return deferred.promise();
}
};
/**
* Initialize ULS front-end and its i18n.
*
* @param {Function} callback callback function to be called after initialization.
*/
mw.uls.init = function ( callback ) {
var messageStore = new MWMessageStore();
callback = callback || $.noop;
if ( initialized ) {
callback.call( this );
return;
}
if ( !isBrowserSupported() ) {
$( '#pt-uls' ).hide();
return;
}
// If EventLogging integration is enabled, set event defaults and make the
// the function call event logging with correct schema.
if ( mw.config.get( 'wgULSEventLogging' ) ) {
mw.loader.using( 'schema.UniversalLanguageSelector', function () {
mw.eventLog.setDefaults( 'UniversalLanguageSelector', {
version: 1,
token: mw.user.id(),
contentLanguage: mw.config.get( 'wgContentLanguage' ),
interfaceLanguage: currentLang
} );
logEventQueue.fire();
} );
}
/*
* The 'als' is used in a non-standard way in MediaWiki -
* it may be used to represent the Allemanic language,
* the standard code of which is 'gsw', while 'als'
* is ISO 639 3 refers to Tosk Albanian, which is
* not currently used in any way in MediaWiki.
* This local fix adds a redirect for it.
*/
$.uls.data.addLanguage( 'als', { target: 'gsw' } );
// JavaScript side i18n initialization
$.i18n( {
locale: currentLang,
messageStore: messageStore
} );
if ( !jsonLoader ) {
jsonLoader = messageStore.load( currentLang );
} else {
jsonLoader.done( function () {
initialized = true;
} );
jsonLoader.done( callback );
}
};
$( document ).ready( function () {
mw.uls.init();
} );
}( jQuery, mediaWiki ) );