MediaWiki talk:Gadget-SiteIdToInterwiki.js

From Wikidata
Jump to navigation Jump to search

jQuery.expr.pseudos[edit]

{{Edit request}}

Can an interface admin please change $.expr[':'] to $.expr.pseudos? The [':'] form is deprecated and causes a warning to be logged to the console; see also phabricator:T185628. --Lucas Werkmeister (WMDE) (talk) 14:59, 18 June 2019 (UTC)[reply]

✓ Done – when I filed the request I wasn’t aware that staff accounts can add/remove the interface admin flag on their account, so I was able to fulfill this request myself :) --Lucas Werkmeister (WMDE) (talk) 18:56, 18 June 2019 (UTC)[reply]

Do not replace "mediawiki"[edit]

{{Edit request}}

@@ -14,7 +14,7 @@
  * @param {string} siteId
  */
 function getLanguageCode( siteId ) {
-	return siteId.replace( /(wiki(books|news|quote|source|versity|voyage)?|wiktionary)$/, '' );
+	return siteId.replace( /(^(?!media))?(wiki(books|news|quote|source|versity|voyage)?|wiktionary)$/, '' );
 }

 /**

mediawiki should not be replaced to media. Stang 21:45, 3 June 2022 (UTC)[reply]

Done, Closing. Stang 11:15, 14 June 2022 (UTC)[reply]

Fixes for deprecated language codes[edit]

{{Edit request}}

Version 1:

@@ -8,13 +8,31 @@
 ( function( $ ) {
 'use strict';
+
+/**
+ * Replace deprecated language code.
+ *
+ * @param {string} langCode
+ */
+function replaceDeprecatedCode( langCode ) {
+	var deprecatedCodeMapping = {
+		be_x_old: 'be-tarask',
+		zh_classical: 'lzh',
+		zh_min_nan: 'nan',
+		zh_yue: 'yue',
+	};
+
+	return deprecatedCodeMapping[langCode] ?? langCode.replaceAll( '_', '-' );
+}

 /**
  * Gets the interwiki code from a site id.
  *
  * @param {string} siteId
  */
 function getLanguageCode( siteId ) {
+	return replaceDeprecatedCode(
-	return siteId.replace( /(^(?!media))?(wiki(books|news|quote|source|versity|voyage)?|wiktionary)$/, '' );
+		siteId.replace( /(^(?!media))?(wiki(books|news|quote|source|versity|voyage)?|wiktionary)$/, '' )
+	);
 }

 /**


Version 2:

@@ -8,13 +8,34 @@
 ( function( $ ) {
 'use strict';
+
+/**
+ * Replace deprecated language code by site id.
+ *
+ * @param {string} id
+ */
+function replaceDeprecatedCode( id ) {
+	var deprecatedCodeMapping = {
+		be_x_oldwiki: 'be_taraskwiki',
+		zh_classicalwiki: 'lzhwiki',
+		zh_min_nanwiki: 'nanwiki',
+		zh_min_nanwikiquote: 'nanwikiquote',
+		zh_min_nanwikisource: 'nanwikisource',
+		zh_min_nanwiktionary: 'nanwiktionary',
+		zh_yuewiki: 'yuewiki',
+	};
+
+	return deprecatedCodeMapping[id] ?? id;
+}

 /**
  * Gets the interwiki code from a site id.
  *
  * @param {string} siteId
  */
 function getLanguageCode( siteId ) {
+	return replaceDeprecatedCode( siteId )
-	return siteId.replace( /(^(?!media))?(wiki(books|news|quote|source|versity|voyage)?|wiktionary)$/, '' );
+		.replace( /(^(?!media))?(wiki(books|news|quote|source|versity|voyage)?|wiktionary)$/, '' )
+		.replaceAll( '_', '-' );
 }

 /**

Already tested both versions, test results for Version 1 & 2: both the fixed code (e.g. be-tarask) and the deprecated code (e.g. be-x-old) works, no matter using underscore (_) or dash (-). --122.100.88.32 16:34, 9 January 2023 (UTC)[reply]

✓ Done Mbch331 (talk) 08:50, 10 January 2023 (UTC)[reply]

Bug fixes for edit request[edit]

{{Edit request}}

Fix 'nullish coalescing' is only available in ES11

@@ -22,4 +22,4 @@
 	};

-	return deprecatedCodeMapping[langCode] ?? langCode.replaceAll( '_', '-' );
+	return deprecatedCodeMapping[langCode] ? deprecatedCodeMapping[langCode] : langCode.replaceAll( '_', '-' );
 }

The resource loader for gadgets can only use ES5, caused the compilation error. --122.116.63.54 04:20, 11 January 2023 (UTC)[reply]

CC @Mbch331 for the bug fix. --122.116.63.54 04:24, 11 January 2023 (UTC)[reply]
✓ Done - Mbch331 (talk) 10:22, 11 January 2023 (UTC)[reply]
@Mbch331: for me this causes "TypeError: "langCode.replaceAll is not a function"", and I can no longer edit terms directly without being redirected to Special:SetLabelDescriptionAliases. Please undo yesterday's and today's changes and test properly before reapplying. 2001:7D0:81FD:BC80:5581:AA67:8D38:A9AF 12:39, 11 January 2023 (UTC)[reply]
✓ Done. @122.116.63.54: Can you check above comment? Mbch331 (talk) 14:49, 11 January 2023 (UTC)[reply]
Fix: Change langCode.replaceAll( '_', '-' ); to langCode.replace( /_/g, '-' );
CC @Mbch331 --122.116.63.54 06:21, 12 January 2023 (UTC)[reply]
I now discovered that replaceAll is from ECMAScript 2021, which is not supported by ES5. 122.116.63.54 06:26, 12 January 2023 (UTC)[reply]
✓ Done - Mbch331 (talk) 09:12, 12 January 2023 (UTC)[reply]

Fixes for deprecated language codes (2)[edit]

{{Edit request}}

The deprecated language code mapping was incomplete.

Please update to the full list defined in LanguageCode::DEPRECATED_LANGUAGE_CODE_MAPPING :

@@ -16,7 +16,11 @@
 function replaceDeprecatedCode( langCode ) {
 	var deprecatedCodeMapping = {
+		als: 'gsw',
+		bat_smg: 'sgs',
 		be_x_old: 'be-tarask',
+		fiu_vro: 'vro',
+		roa_rup: 'rup',
 		zh_classical: 'lzh',
 		zh_min_nan: 'nan',
 		zh_yue: 'yue',
 	};

--36.225.120.244 16:09, 27 January 2023 (UTC)[reply]

CC @Mbch331 -- 36.225.121.14 20:32, 29 January 2023 (UTC)[reply]
✓ Done - Mbch331 (talk) 20:45, 29 January 2023 (UTC)[reply]
I had to remove the mapping for als/gsw, see phab:T331113. Mbch331 (talk) 18:54, 21 March 2023 (UTC)[reply]

Fixes for deprecated language codes and sorting issue[edit]

It is requested that an edit or modification be made to this protected page.

The page has been added to Category:Wikidata protected edit requests.

Administrators: Please apply <nowiki> or {{Tl}} to the tag after the request is fulfilled.

Fixed sorting issue in phab:T331113, CC @Mbch331:

/**
 * Replaces Wikibase site id with interwiki code.
 * Examples: "enwiki" -> "en", "enwikiquote" -> "en", "commonswiki" -> "commons"
 *
 * @uses jQuery.wikibase.sitelinklistview
 * @uses jQuery.wikibase.sitelinkview
 */
( function( $ ) {
	'use strict';

	/**
	 * Replace deprecated language code.
	 *
	 * @param {string} langCode
	 */
	function replaceDeprecatedCode( langCode ) {
		var deprecatedCodeMapping = {
			als: 'gsw',
			bat_smg: 'sgs',
			be_x_old: 'be-tarask',
			eml: 'egl',
			fiu_vro: 'vro',
			'no': 'nb',
			roa_rup: 'rup',
			simple: 'en-simple',
			zh_classical: 'lzh',
			zh_min_nan: 'nan',
			zh_yue: 'yue',
		};

		return deprecatedCodeMapping[langCode] ?
			deprecatedCodeMapping[langCode] : langCode.replace( /_/g, '-' );
	}

	/**
	 * Gets the interwiki code from a site id.
	 *
	 * @param {string} siteId
	 */
	function getLanguageCode( siteId ) {
		return replaceDeprecatedCode( siteId.replace(
				/(^(?!media))?(wiki(books|news|quote|source|versity|voyage)?|wiktionary)$/,
				''
		) );
	}

	/**
	 * Replaces the site id with the interwiki code.
	 *
	 * @param {jQuery} $siteId
	 */
	function replaceSiteId( $siteId ) {
		$siteId.text( getLanguageCode( $siteId.text() ) );
	}

	/**
	 * Fix sorting after deprecated language codes were replaced.
	 */
	function fixSiteLinkListViewSorting() {
		var lists = document.querySelectorAll( '.wikibase-sitelinklistview-listview' );

		for ( var listIndex = 0; listIndex < lists.length; listIndex++ ) {
			var items = lists[listIndex].childNodes;
			var itemsArr = [];
			for ( var itemIndex = 0; itemIndex < items.length; itemIndex++ ) {
				itemsArr.push( items[itemIndex] );
			}

			itemsArr.sort( function( a, b ) {
				var keyA = a.innerText;
				var keyB = b.innerText;
				return keyA == keyB ? 0 : ( keyA > keyB ? 1 : -1 );
			} );

			for ( var itemArrIndex = 0; itemArrIndex < itemsArr.length; ++itemArrIndex ) {
				lists[listIndex].appendChild( itemsArr[itemArrIndex] );
			}
		}
	}

	/**
	 * Overwrites native `jQuery.wikibase.sitelinkview` implementation.
	 *
	 * @constructor
	 */
	$.widget( 'wikibase.sitelinkview', $.wikibase.sitelinkview, {
		/**
		 * @see jQuery.wikibase.sitelinkview._create
		 */
		__nativeCreate: $.wikibase.sitelinkview.prototype._create,

		/**
		 * @inheritdoc
		 */
		_create: function() {
			this.__nativeCreate.call( this );
			replaceSiteId( this.$siteId );
		}
	} );

	/**
	 * Overwrites native `jQuery.wikibase.siteselector` implementation.
	 *
	 * @constructor
	 */
	$.widget( 'wikibase.siteselector', $.wikibase.siteselector, {
		/**
		 * @inheritdoc
		 */
		_createItemLabel: function( site, requestTerm ) {
			return util.highlightSubstring( requestTerm, site.getShortName() ) +
				' (' + util.highlightSubstring( requestTerm, this._getCode( site ) ) + ')';
		},

		/**
		 * @inheritdoc
		 */
		_considerSuggestion: function( site ) {
			var code = this._getCode( site );

			var check = [
				code,
				site.getShortName(),
				site.getName(),
				site.getShortName() + ' (' + code + ')'
			];
	
			for( var i = 0; i < check.length; i++ ) {
				if ( check[i].toLowerCase().indexOf( this._term.toLowerCase() ) === 0 ) {
					return true;
				}
			}

			return false;
		},

		/**
		 * @inheritdoc
		 */
		_createItemValue: function( site ) {
			return this._getCode( site );
		},

		/**
		 * Returns the interwiki code of a specified site.
		 * @private
		 *
		 * @param {wikibase.Site} site
		 * @return {string}
		 */
		_getCode: function( site ) {
			return getLanguageCode( site.getId() );
		}
	} );

	// Replace site ids in rendered HTML:
	$( '.wikibase-sitelinkview .wikibase-sitelinkview-siteid' ).each( function() {
		replaceSiteId( $( this ) );
	} );

	// Fix sorting after replaced site ids
	fixSiteLinkListViewSorting();

	// Adjust sitelinklistview widgets that have been initialized already to use the overwritten
	// sitelinkview widget:
	if( $.expr.pseudos['wikibase-sitelinklistview'] ) {
		$( ':wikibase-sitelinklistview' ).each( function() {
			var sitelinklistview = $( this ).data( 'sitelinklistview' );
			sitelinklistview.$listview.data( 'listview' ).listItemAdapter()._liWidget =
				$.wikibase.sitelinkview;
		} );
	}

} ( jQuery ) );

-- 122.100.88.89 05:32, 18 January 2024 (UTC)[reply]