MediaWiki:Gadget-SetTestWikiPreference.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Set test wiki preference gadget
 * 
 * This gadget adds some simplifying bits to the Wikimedia Incubator:
 * * Adds "Set language" button to the sidebar, to easily change interface 
 *   language preferences to that of the viewed test project.
 * * Adds "Set (test project) as your test project" button to the sidebar.
 * 
 * The gadget works in all skins, both on desktop and mobile.
 *ne
 * @author Yair rand
 * @author Jon Harald Søby
 * @version 1.4.4 (2024-03-21)
 */

( function () {
	var userTestProjectWiki = mw.user.options.get( "incubatortestwiki-project" ),
		userTestProjectLang = mw.user.options.get( "incubatortestwiki-code" ),
		userTestProject = "W" + userTestProjectWiki + "/" + userTestProjectLang,
		prefix = mw.config.get( 'wgWmincTestwikiPrefix' ),
		userLang = mw.config.get( 'wgUserLanguage' ),
		testProject = mw.config.get( 'wgWmincTestwikiProject' ),
		testLang = mw.config.get( 'wgWmincTestwikiLanguage' ),
		testLangName = mw.language.getData( userLang, 'languageNames' )[ testLang ] || testLang,
		api = new mw.Api();

	if ( !mw.user.isAnon() ) {
		api.loadMessagesIfMissing( [
			'wminc-gadget-twpref-setlang',
			'wminc-gadget-twpref-setlang-tooltip',
			'wminc-gadget-twpref-setwiki',
			'wminc-gadget-twpref-setwiki-tooltip'
		] ).then( function() {
			var portletSection = 'p-navigation',
				nextNode = null,
				displaySetlangLink = ( testLang &&
					testLang !== userLang &&
					// Whether the language is supported in MediaWiki
					!!mw.config.get( 'wgULSLanguages' )[ testLang ]
				);

			if ( mw.config.get( 'skin' ) === 'minerva' ) {
				// Add styling for the Minerva skin
				mw.util.addCSS(
					'.minerva-icon-portletlink-t-setlang {' +
					'  background-image: url(/w/load.php?modules=' +
					'skins.minerva.icons.wikimedia&image=language&' +
					'format=original&skin=minerva&version=dmrkv);' +
					'} .minerva-icon-portletlink-t-sethomewiki {' +
					'  background-image: url(//upload.wikimedia.org/' +
					'wikipedia/commons/c/c3/Incubator-logo-gray.svg) }'
				);

				portletSection = 'p-personal';
				if ( mw.user.options.get( 'mf_amc_optin' ) === '1' ) {
					portletSection = 'pt-preferences';
				} else {
					nextNode = $( '[data-event-name="menu.logout"]' ).parent();
				}
			}

			// Add a sidebar link to switch interface language to that of the
			// currently viewed test project.
			if ( displaySetlangLink ) {
				mw.util.addPortletLink(
					portletSection,
					mw.util.getUrl( null, { setlang: testLang } ),
					mw.msg( "wminc-gadget-twpref-setlang", testLangName ), 
					"t-setlang",
					mw.msg( "wminc-gadget-twpref-setlang-tooltip", testLangName ),
					null,
					nextNode
				);
			}

			// Add a sidebar link to make the currently viewed test project the
			// user's test project.
			if ( prefix && prefix !== userTestProject ) {
				$( mw.util.addPortletLink(
					portletSection,
					"#set-project-" + prefix, // Link to this anchor, no particular reason.
					mw.msg( "wminc-gadget-twpref-setwiki", prefix ),
					"t-sethomewiki",
					mw.msg( "wminc-gadget-twpref-setwiki-tooltip", prefix ),
					null,
					nextNode
				) ).on( 'click', function () {
					// Send the preferences change to the server.
					api.postWithEditToken( {
						action: 'options',
						format: 'json',
						change: 'incubatortestwiki-project=' + testProject + '|' +
								'incubatortestwiki-code=' + testLang
					} ).then( function() {
						location.reload();
					} );
				} );	
			}
		} );
	}
} )();